Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Alex Sakhartchouk | 77d9f4b | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 17 | #include "rsContext.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include "rsSampler.h" |
Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 19 | #include "rs.h" |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 20 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
| 24 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 25 | Sampler::Sampler(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | // Should not get called. |
| 27 | rsAssert(0); |
| 28 | } |
| 29 | |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 30 | Sampler::Sampler(Context *rsc, |
| 31 | RsSamplerValue magFilter, |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | RsSamplerValue minFilter, |
| 33 | RsSamplerValue wrapS, |
| 34 | RsSamplerValue wrapT, |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 35 | RsSamplerValue wrapR, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 36 | float aniso) : ObjectBase(rsc) { |
Alex Sakhartchouk | c2c02a8 | 2011-05-04 17:45:36 -0700 | [diff] [blame] | 37 | mHal.state.magFilter = magFilter; |
| 38 | mHal.state.minFilter = minFilter; |
| 39 | mHal.state.wrapS = wrapS; |
| 40 | mHal.state.wrapT = wrapT; |
| 41 | mHal.state.wrapR = wrapR; |
| 42 | mHal.state.aniso = aniso; |
Alex Sakhartchouk | 7f126c7 | 2011-05-05 16:56:27 -0700 | [diff] [blame] | 43 | |
| 44 | mRSC->mHal.funcs.sampler.init(mRSC, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 47 | Sampler::~Sampler() { |
Alex Sakhartchouk | 7f126c7 | 2011-05-05 16:56:27 -0700 | [diff] [blame] | 48 | mRSC->mHal.funcs.sampler.destroy(mRSC, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 51 | void Sampler::preDestroy() const { |
Chris Wailes | 93d6bc8 | 2014-07-28 16:54:38 -0700 | [diff] [blame] | 52 | auto &samplers = mRSC->mStateSampler.mAllSamplers; |
| 53 | |
| 54 | for (auto sampleIter = samplers.begin(), endIter = samplers.end(); |
| 55 | sampleIter != endIter; sampleIter++) { |
| 56 | |
| 57 | if (this == *sampleIter) { |
| 58 | samplers.erase(sampleIter); |
| 59 | return; |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 64 | void Sampler::bindToContext(SamplerState *ss, uint32_t slot) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 65 | ss->mSamplers[slot].set(this); |
| 66 | mBoundSlot = slot; |
| 67 | } |
| 68 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 69 | void Sampler::unbindFromContext(SamplerState *ss) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 70 | int32_t slot = mBoundSlot; |
| 71 | mBoundSlot = -1; |
| 72 | ss->mSamplers[slot].clear(); |
| 73 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 74 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 75 | void Sampler::serialize(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 78 | Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame^] | 79 | return nullptr; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 82 | ObjectBaseRef<Sampler> Sampler::getSampler(Context *rsc, |
| 83 | RsSamplerValue magFilter, |
| 84 | RsSamplerValue minFilter, |
| 85 | RsSamplerValue wrapS, |
| 86 | RsSamplerValue wrapT, |
| 87 | RsSamplerValue wrapR, |
| 88 | float aniso) { |
| 89 | ObjectBaseRef<Sampler> returnRef; |
| 90 | ObjectBase::asyncLock(); |
| 91 | for (uint32_t ct = 0; ct < rsc->mStateSampler.mAllSamplers.size(); ct++) { |
| 92 | Sampler *existing = rsc->mStateSampler.mAllSamplers[ct]; |
| 93 | if (existing->mHal.state.magFilter != magFilter) continue; |
| 94 | if (existing->mHal.state.minFilter != minFilter ) continue; |
| 95 | if (existing->mHal.state.wrapS != wrapS) continue; |
| 96 | if (existing->mHal.state.wrapT != wrapT) continue; |
| 97 | if (existing->mHal.state.wrapR != wrapR) continue; |
| 98 | if (existing->mHal.state.aniso != aniso) continue; |
| 99 | returnRef.set(existing); |
| 100 | ObjectBase::asyncUnlock(); |
| 101 | return returnRef; |
| 102 | } |
| 103 | ObjectBase::asyncUnlock(); |
| 104 | |
Ling Wan | 3abc05b | 2013-03-15 16:21:50 -0700 | [diff] [blame] | 105 | void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Sampler), 0); |
| 106 | if (!allocMem) { |
| 107 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation"); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame^] | 108 | return nullptr; |
Ling Wan | 3abc05b | 2013-03-15 16:21:50 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | Sampler *s = new (allocMem) Sampler(rsc, magFilter, minFilter, wrapS, wrapT, wrapR, aniso); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 112 | returnRef.set(s); |
| 113 | |
Tim Murray | e3af53b | 2014-06-10 09:46:51 -0700 | [diff] [blame] | 114 | #ifdef RS_FIND_OFFSETS |
| 115 | ALOGE("pointer for sampler: %p", s); |
| 116 | ALOGE("pointer for sampler.drv: %p", &s->mHal.drv); |
| 117 | #endif |
| 118 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 119 | ObjectBase::asyncLock(); |
Chris Wailes | 93d6bc8 | 2014-07-28 16:54:38 -0700 | [diff] [blame] | 120 | rsc->mStateSampler.mAllSamplers.push_back(s); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 121 | ObjectBase::asyncUnlock(); |
| 122 | |
| 123 | return returnRef; |
| 124 | } |
| 125 | |
Ling Wan | 3abc05b | 2013-03-15 16:21:50 -0700 | [diff] [blame] | 126 | void Sampler::operator delete(void* ptr) { |
| 127 | if (ptr) { |
| 128 | Sampler *s = (Sampler*) ptr; |
| 129 | s->getContext()->mHal.funcs.freeRuntimeMem(ptr); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 134 | //////////////////////////////// |
| 135 | |
| 136 | namespace android { |
| 137 | namespace renderscript { |
| 138 | |
Alex Sakhartchouk | c2c02a8 | 2011-05-04 17:45:36 -0700 | [diff] [blame] | 139 | RsSampler rsi_SamplerCreate(Context * rsc, |
| 140 | RsSamplerValue magFilter, |
| 141 | RsSamplerValue minFilter, |
| 142 | RsSamplerValue wrapS, |
| 143 | RsSamplerValue wrapT, |
| 144 | RsSamplerValue wrapR, |
| 145 | float aniso) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 146 | ObjectBaseRef<Sampler> s = Sampler::getSampler(rsc, magFilter, minFilter, |
| 147 | wrapS, wrapT, wrapR, aniso); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 148 | s->incUserRef(); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 149 | return s.get(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 152 | }} |