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