rsProgramStore.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2009 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. #include "rsContext.h"
  17. #include "rsProgramStore.h"
  18. namespace android {
  19. namespace renderscript {
  20. ProgramStore::ProgramStore(Context *rsc,
  21. bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
  22. bool depthMask, bool ditherEnable,
  23. RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
  24. RsDepthFunc depthFunc) : ProgramBase(rsc) {
  25. memset(&mHal, 0, sizeof(mHal));
  26. mHal.state.ditherEnable = ditherEnable;
  27. mHal.state.colorRWriteEnable = colorMaskR;
  28. mHal.state.colorGWriteEnable = colorMaskG;
  29. mHal.state.colorBWriteEnable = colorMaskB;
  30. mHal.state.colorAWriteEnable = colorMaskA;
  31. mHal.state.blendSrc = srcFunc;
  32. mHal.state.blendDst = destFunc;
  33. mHal.state.depthWriteEnable = depthMask;
  34. mHal.state.depthFunc = depthFunc;
  35. }
  36. void ProgramStore::preDestroy() const {
  37. auto& storePrograms = mRSC->mStateFragmentStore.mStorePrograms;
  38. for (uint32_t ct = 0; ct < storePrograms.size(); ct++) {
  39. if (storePrograms[ct] == this) {
  40. storePrograms.erase(storePrograms.begin() + ct);
  41. break;
  42. }
  43. }
  44. }
  45. ProgramStore::~ProgramStore() {
  46. mRSC->mHal.funcs.store.destroy(mRSC, this);
  47. }
  48. void ProgramStore::setup(const Context *rsc, ProgramStoreState *state) {
  49. if (state->mLast.get() == this) {
  50. return;
  51. }
  52. state->mLast.set(this);
  53. rsc->mHal.funcs.store.setActive(rsc, this);
  54. }
  55. void ProgramStore::serialize(Context *rsc, OStream *stream) const {
  56. }
  57. ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) {
  58. return nullptr;
  59. }
  60. void ProgramStore::init() {
  61. mRSC->mHal.funcs.store.init(mRSC, this);
  62. }
  63. ProgramStoreState::ProgramStoreState() {
  64. }
  65. ProgramStoreState::~ProgramStoreState() {
  66. }
  67. ObjectBaseRef<ProgramStore> ProgramStore::getProgramStore(Context *rsc,
  68. bool colorMaskR,
  69. bool colorMaskG,
  70. bool colorMaskB,
  71. bool colorMaskA,
  72. bool depthMask, bool ditherEnable,
  73. RsBlendSrcFunc srcFunc,
  74. RsBlendDstFunc destFunc,
  75. RsDepthFunc depthFunc) {
  76. ObjectBaseRef<ProgramStore> returnRef;
  77. ObjectBase::asyncLock();
  78. for (uint32_t ct = 0; ct < rsc->mStateFragmentStore.mStorePrograms.size(); ct++) {
  79. ProgramStore *existing = rsc->mStateFragmentStore.mStorePrograms[ct];
  80. if (existing->mHal.state.ditherEnable != ditherEnable) continue;
  81. if (existing->mHal.state.colorRWriteEnable != colorMaskR) continue;
  82. if (existing->mHal.state.colorGWriteEnable != colorMaskG) continue;
  83. if (existing->mHal.state.colorBWriteEnable != colorMaskB) continue;
  84. if (existing->mHal.state.colorAWriteEnable != colorMaskA) continue;
  85. if (existing->mHal.state.blendSrc != srcFunc) continue;
  86. if (existing->mHal.state.blendDst != destFunc) continue;
  87. if (existing->mHal.state.depthWriteEnable != depthMask) continue;
  88. if (existing->mHal.state.depthFunc != depthFunc) continue;
  89. returnRef.set(existing);
  90. ObjectBase::asyncUnlock();
  91. return returnRef;
  92. }
  93. ObjectBase::asyncUnlock();
  94. ProgramStore *pfs = new ProgramStore(rsc,
  95. colorMaskR, colorMaskG, colorMaskB, colorMaskA,
  96. depthMask, ditherEnable,
  97. srcFunc, destFunc, depthFunc);
  98. returnRef.set(pfs);
  99. pfs->init();
  100. ObjectBase::asyncLock();
  101. rsc->mStateFragmentStore.mStorePrograms.push_back(pfs);
  102. ObjectBase::asyncUnlock();
  103. return returnRef;
  104. }
  105. void ProgramStoreState::init(Context *rsc) {
  106. mDefault.set(ProgramStore::getProgramStore(rsc,
  107. true, true, true, true,
  108. true, true,
  109. RS_BLEND_SRC_ONE, RS_BLEND_DST_ZERO,
  110. RS_DEPTH_FUNC_LESS).get());
  111. }
  112. void ProgramStoreState::deinit(Context *rsc) {
  113. mDefault.clear();
  114. mLast.clear();
  115. }
  116. RsProgramStore rsi_ProgramStoreCreate(Context *rsc,
  117. bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
  118. bool depthMask, bool ditherEnable,
  119. RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
  120. RsDepthFunc depthFunc) {
  121. ObjectBaseRef<ProgramStore> ps = ProgramStore::getProgramStore(rsc,
  122. colorMaskR, colorMaskG,
  123. colorMaskB, colorMaskA,
  124. depthMask, ditherEnable,
  125. srcFunc, destFunc, depthFunc);
  126. ps->incUserRef();
  127. return ps.get();
  128. }
  129. } // namespace renderscript
  130. } // namespace android