rsSampler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "rsSampler.h"
  18. #include "rs.h"
  19. namespace android {
  20. namespace renderscript {
  21. Sampler::Sampler(Context *rsc) : ObjectBase(rsc) {
  22. // Should not get called.
  23. rsAssert(0);
  24. }
  25. Sampler::Sampler(Context *rsc,
  26. RsSamplerValue magFilter,
  27. RsSamplerValue minFilter,
  28. RsSamplerValue wrapS,
  29. RsSamplerValue wrapT,
  30. RsSamplerValue wrapR,
  31. float aniso) : ObjectBase(rsc) {
  32. mHal.state.magFilter = magFilter;
  33. mHal.state.minFilter = minFilter;
  34. mHal.state.wrapS = wrapS;
  35. mHal.state.wrapT = wrapT;
  36. mHal.state.wrapR = wrapR;
  37. mHal.state.aniso = aniso;
  38. mRSC->mHal.funcs.sampler.init(mRSC, this);
  39. }
  40. Sampler::~Sampler() {
  41. mRSC->mHal.funcs.sampler.destroy(mRSC, this);
  42. }
  43. void Sampler::preDestroy() const {
  44. auto& allSamplers = mRSC->mStateSampler.mAllSamplers;
  45. for (uint32_t ct = 0; ct < allSamplers.size(); ct++) {
  46. if (allSamplers[ct] == this) {
  47. allSamplers.erase(allSamplers.begin() + ct);
  48. break;
  49. }
  50. }
  51. }
  52. void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
  53. ss->mSamplers[slot].set(this);
  54. mBoundSlot = slot;
  55. }
  56. void Sampler::unbindFromContext(SamplerState *ss) {
  57. int32_t slot = mBoundSlot;
  58. mBoundSlot = -1;
  59. ss->mSamplers[slot].clear();
  60. }
  61. void Sampler::serialize(Context *rsc, OStream *stream) const {
  62. }
  63. Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
  64. return nullptr;
  65. }
  66. ObjectBaseRef<Sampler> Sampler::getSampler(Context *rsc,
  67. RsSamplerValue magFilter,
  68. RsSamplerValue minFilter,
  69. RsSamplerValue wrapS,
  70. RsSamplerValue wrapT,
  71. RsSamplerValue wrapR,
  72. float aniso) {
  73. ObjectBaseRef<Sampler> returnRef;
  74. ObjectBase::asyncLock();
  75. for (uint32_t ct = 0; ct < rsc->mStateSampler.mAllSamplers.size(); ct++) {
  76. Sampler *existing = rsc->mStateSampler.mAllSamplers[ct];
  77. if (existing->mHal.state.magFilter != magFilter) continue;
  78. if (existing->mHal.state.minFilter != minFilter ) continue;
  79. if (existing->mHal.state.wrapS != wrapS) continue;
  80. if (existing->mHal.state.wrapT != wrapT) continue;
  81. if (existing->mHal.state.wrapR != wrapR) continue;
  82. if (existing->mHal.state.aniso != aniso) continue;
  83. returnRef.set(existing);
  84. ObjectBase::asyncUnlock();
  85. return returnRef;
  86. }
  87. ObjectBase::asyncUnlock();
  88. void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Sampler), 0);
  89. if (!allocMem) {
  90. rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
  91. return nullptr;
  92. }
  93. Sampler *s = new (allocMem) Sampler(rsc, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
  94. returnRef.set(s);
  95. #ifdef RS_FIND_OFFSETS
  96. ALOGE("pointer for sampler: %p", s);
  97. ALOGE("pointer for sampler.drv: %p", &s->mHal.drv);
  98. #endif
  99. ObjectBase::asyncLock();
  100. rsc->mStateSampler.mAllSamplers.push_back(s);
  101. ObjectBase::asyncUnlock();
  102. return returnRef;
  103. }
  104. void Sampler::operator delete(void* ptr) {
  105. if (ptr) {
  106. Sampler *s = (Sampler*) ptr;
  107. s->getContext()->mHal.funcs.freeRuntimeMem(ptr);
  108. }
  109. }
  110. ////////////////////////////////
  111. RsSampler rsi_SamplerCreate(Context * rsc,
  112. RsSamplerValue magFilter,
  113. RsSamplerValue minFilter,
  114. RsSamplerValue wrapS,
  115. RsSamplerValue wrapT,
  116. RsSamplerValue wrapR,
  117. float aniso) {
  118. ObjectBaseRef<Sampler> s = Sampler::getSampler(rsc, magFilter, minFilter,
  119. wrapS, wrapT, wrapR, aniso);
  120. s->incUserRef();
  121. return s.get();
  122. }
  123. } // namespace renderscript
  124. } // namespace android