blob: cd90a3215de200e65967178978774cbe3b72a3ef [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include "rsSampler.h"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080019#include "rs.h"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070020
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080021namespace android {
22namespace renderscript {
Jason Sams326e0dd2009-05-22 14:03:28 -070023
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024Sampler::Sampler(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070025 // Should not get called.
26 rsAssert(0);
27}
28
Jason Samse514b452009-09-25 14:51:22 -070029Sampler::Sampler(Context *rsc,
30 RsSamplerValue magFilter,
Jason Sams326e0dd2009-05-22 14:03:28 -070031 RsSamplerValue minFilter,
32 RsSamplerValue wrapS,
33 RsSamplerValue wrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070034 RsSamplerValue wrapR,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080035 float aniso) : ObjectBase(rsc) {
Alex Sakhartchoukc2c02a82011-05-04 17:45:36 -070036 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;
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -070042
43 mRSC->mHal.funcs.sampler.init(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070044}
45
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080046Sampler::~Sampler() {
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -070047 mRSC->mHal.funcs.sampler.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070048}
49
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070050void Sampler::preDestroy() const {
Miao Wang82e135c2017-02-27 23:35:35 -080051 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);
Yang Nib8353c52015-02-14 18:00:59 -080055 break;
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070056 }
57 }
58}
59
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -070061 ss->mSamplers[slot].set(this);
62 mBoundSlot = slot;
63}
64
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065void Sampler::unbindFromContext(SamplerState *ss) {
Jason Sams326e0dd2009-05-22 14:03:28 -070066 int32_t slot = mBoundSlot;
67 mBoundSlot = -1;
68 ss->mSamplers[slot].clear();
69}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070070
Jason Samse3150cf2012-07-24 18:10:20 -070071void Sampler::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070072}
73
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080074Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070075 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070076}
77
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070078ObjectBaseRef<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
Ling Wan3abc05b2013-03-15 16:21:50 -0700101 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");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700104 return nullptr;
Ling Wan3abc05b2013-03-15 16:21:50 -0700105 }
106
107 Sampler *s = new (allocMem) Sampler(rsc, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700108 returnRef.set(s);
109
Tim Murraye3af53b2014-06-10 09:46:51 -0700110#ifdef RS_FIND_OFFSETS
111 ALOGE("pointer for sampler: %p", s);
112 ALOGE("pointer for sampler.drv: %p", &s->mHal.drv);
113#endif
114
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700115 ObjectBase::asyncLock();
Miao Wang82e135c2017-02-27 23:35:35 -0800116 rsc->mStateSampler.mAllSamplers.push_back(s);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700117 ObjectBase::asyncUnlock();
118
119 return returnRef;
120}
121
Ling Wan3abc05b2013-03-15 16:21:50 -0700122void Sampler::operator delete(void* ptr) {
123 if (ptr) {
124 Sampler *s = (Sampler*) ptr;
125 s->getContext()->mHal.funcs.freeRuntimeMem(ptr);
126 }
127}
128
129
Jason Sams326e0dd2009-05-22 14:03:28 -0700130////////////////////////////////
131
Alex Sakhartchoukc2c02a82011-05-04 17:45:36 -0700132RsSampler rsi_SamplerCreate(Context * rsc,
133 RsSamplerValue magFilter,
134 RsSamplerValue minFilter,
135 RsSamplerValue wrapS,
136 RsSamplerValue wrapT,
137 RsSamplerValue wrapR,
138 float aniso) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700139 ObjectBaseRef<Sampler> s = Sampler::getSampler(rsc, magFilter, minFilter,
140 wrapS, wrapT, wrapR, aniso);
Jason Sams9397e302009-08-27 20:23:34 -0700141 s->incUserRef();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700142 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700143}
144
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800145} // namespace renderscript
146} // namespace android