1 /*
2  * Copyright (C) 2008-2012 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 "RenderScript.h"
18 #include "rsCppInternal.h"
19 
20 using android::RSC::Sampler;
21 using android::RSC::sp;
22 
Sampler(sp<RS> rs,void * id)23 Sampler::Sampler(sp<RS> rs, void* id):
24     BaseObj(id, rs)
25 {
26     RsSamplerValue mMin = RS_SAMPLER_INVALID;
27     RsSamplerValue mMag = RS_SAMPLER_INVALID;
28     RsSamplerValue mWrapS = RS_SAMPLER_INVALID;
29     RsSamplerValue mWrapT = RS_SAMPLER_INVALID;
30     float mAniso = 0.f;
31 }
32 
Sampler(sp<RS> rs,void * id,RsSamplerValue min,RsSamplerValue mag,RsSamplerValue wrapS,RsSamplerValue wrapT,float anisotropy)33 Sampler::Sampler(sp<RS> rs, void* id, RsSamplerValue min, RsSamplerValue mag,
34                  RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy):
35     BaseObj(id, rs)
36 {
37     RsSamplerValue mMin = min;
38     RsSamplerValue mMag = mag;
39     RsSamplerValue mWrapS = wrapS;
40     RsSamplerValue mWrapT = wrapT;
41     float mAniso = anisotropy;
42 }
43 
getMinification()44 RsSamplerValue Sampler::getMinification() {
45     return mMin;
46 }
47 
getMagnification()48 RsSamplerValue Sampler::getMagnification() {
49     return mMag;
50 }
51 
getWrapS()52 RsSamplerValue Sampler::getWrapS() {
53     return mWrapS;
54 }
55 
getWrapT()56 RsSamplerValue Sampler::getWrapT() {
57     return mWrapT;
58 }
59 
getAnisotropy()60 float Sampler::getAnisotropy() {
61     return mAniso;
62 }
63 
create(const sp<RS> & rs,RsSamplerValue min,RsSamplerValue mag,RsSamplerValue wrapS,RsSamplerValue wrapT,float anisotropy)64 sp<Sampler> Sampler::create(const sp<RS>& rs, RsSamplerValue min, RsSamplerValue mag,
65                             RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy) {
66     // We aren't supporting wrapR in C++ API atm, so always pass wrap for that.
67     void* id = RS::dispatch->SamplerCreate(rs->getContext(), min, mag, wrapS, wrapT,
68                                            RS_SAMPLER_WRAP, anisotropy);
69     return new Sampler(rs, id, min, mag, wrapS, wrapT, anisotropy);
70 }
71 
72 #define CREATE_SAMPLER(N, MIN, MAG, WRAPS, WRAPT) sp<const Sampler> Sampler::N(const sp<RS> &rs) { \
73         if (rs->mSamplers.N == nullptr) {                                \
74             rs->mSamplers.N = (create(rs, MIN, MAG, WRAPS, WRAPT, 0.f)); \
75         }                                                                \
76         return rs->mSamplers.N;                                          \
77     }
78 
79 CREATE_SAMPLER(CLAMP_NEAREST, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP);
80 CREATE_SAMPLER(CLAMP_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP);
81 CREATE_SAMPLER(CLAMP_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP);
82 CREATE_SAMPLER(WRAP_NEAREST, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP);
83 CREATE_SAMPLER(WRAP_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP);
84 CREATE_SAMPLER(WRAP_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP);
85 CREATE_SAMPLER(MIRRORED_REPEAT_NEAREST, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT);
86 CREATE_SAMPLER(MIRRORED_REPEAT_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT);
87 CREATE_SAMPLER(MIRRORED_REPEAT_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT);
88