Sampler.java revision ce8b0e674c93035013d1c33aaabc9bb6ceffde0f
198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/*
298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Copyright (C) 2012 The Android Open Source Project
398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * you may not use this file except in compliance with the License.
698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * You may obtain a copy of the License at
798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
1098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Unless required by applicable law or agreed to in writing, software
1198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * See the License for the specific language governing permissions and
1498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * limitations under the License.
1598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */
1698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspackage android.support.v8.renderscript;
1898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport java.io.IOException;
2198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport java.io.InputStream;
2298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.res.Resources;
2498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.os.Bundle;
2598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.util.Log;
2698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.Bitmap;
2898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.BitmapFactory;
2998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
3098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/**
3198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Sampler object which defines how data is extracted from textures. Samplers
3298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * are attached to Program objects (currently only ProgramFragment) when those objects
3398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * need to access texture data.
3498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/
3598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class Sampler extends BaseObj {
3698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public enum Value {
3798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        NEAREST (0),
3898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LINEAR (1),
3998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LINEAR_MIP_LINEAR (2),
4098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LINEAR_MIP_NEAREST (5),
4198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        WRAP (3),
42626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        CLAMP (4),
43626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        MIRRORED_REPEAT (6);
4498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
4598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int mID;
4698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value(int id) {
4798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mID = id;
4898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
4998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
5098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mMin;
5298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mMag;
5398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mWrapS;
5498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mWrapT;
5598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mWrapR;
5698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    float mAniso;
5798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
5898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler(int id, RenderScript rs) {
5998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        super(id, rs);
6098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
6198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
6398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return minification setting for the sampler
6498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
6598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getMinification() {
6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mMin;
6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return magnification setting for the sampler
7198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
7298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getMagnification() {
7398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mMag;
7498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
7598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
7698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return S wrapping mode for the sampler
7898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
7998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getWrapS() {
8098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mWrapS;
8198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
8298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return T wrapping mode for the sampler
8598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
8698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getWrapT() {
8798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mWrapT;
8898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
8998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
9098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return anisotropy setting for the sampler
9298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
9398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public float getAnisotropy() {
9498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mAniso;
9598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
9698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
9798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Retrieve a sampler with min and mag set to nearest and wrap modes set to
9998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * clamp.
10098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
10198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
10298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
10398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
10498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
10598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler CLAMP_NEAREST(RenderScript rs) {
10698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_CLAMP_NEAREST == null) {
10798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
10898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.NEAREST);
10998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.NEAREST);
11098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.CLAMP);
11198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.CLAMP);
11298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_CLAMP_NEAREST = b.create();
11398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
11498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_CLAMP_NEAREST;
11598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
11698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
11798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Retrieve a sampler with min and mag set to linear and wrap modes set to
11998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * clamp.
12098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
12298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
12498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
12598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler CLAMP_LINEAR(RenderScript rs) {
12698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_CLAMP_LINEAR == null) {
12798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
12898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR);
12998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
13098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.CLAMP);
13198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.CLAMP);
13298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_CLAMP_LINEAR = b.create();
13398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
13498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_CLAMP_LINEAR;
13598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
13698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
13798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
138f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
139626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * wrap modes set to clamp.
14098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
14198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
14298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
14398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
14498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
14598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
14698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
14798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
14898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR_MIP_LINEAR);
14998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
15098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.CLAMP);
15198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.CLAMP);
15298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
15398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
15498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
15598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
15698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
15798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
15898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Retrieve a sampler with min and mag set to nearest and wrap modes set to
15998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * wrap.
16098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
16198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
16298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
16398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
16498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
16598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler WRAP_NEAREST(RenderScript rs) {
16698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_WRAP_NEAREST == null) {
16798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
16898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.NEAREST);
16998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.NEAREST);
17098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.WRAP);
17198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.WRAP);
17298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_WRAP_NEAREST = b.create();
17398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
17498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_WRAP_NEAREST;
17598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
17698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
17798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
178f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * Retrieve a sampler with min and mag set to linear and wrap modes set to
17998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * wrap.
18098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
18198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
18298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
18398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
18498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
18598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler WRAP_LINEAR(RenderScript rs) {
18698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_WRAP_LINEAR == null) {
18798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
18898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR);
18998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
19098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.WRAP);
19198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.WRAP);
19298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_WRAP_LINEAR = b.create();
19398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
19498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_WRAP_LINEAR;
19598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
19698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
19798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
198f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
199626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * wrap modes set to wrap.
20098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
20198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
20298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
20398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
20498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
20598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
20698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
20798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
20898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR_MIP_LINEAR);
20998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
21098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.WRAP);
21198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.WRAP);
21298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
21398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
21498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
21598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
21698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
217626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    /**
218626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * Retrieve a sampler with min and mag set to nearest and wrap modes set to
219626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * mirrored repeat.
220626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
221626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @param rs Context to which the sampler will belong.
222626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
223626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @return Sampler
224626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     */
225626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
226626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
227626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            Builder b = new Builder(rs);
228626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMinification(Value.NEAREST);
229626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMagnification(Value.NEAREST);
230626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapS(Value.MIRRORED_REPEAT);
231626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapT(Value.MIRRORED_REPEAT);
232626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
233626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        }
234626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        return rs.mSampler_MIRRORED_REPEAT_NEAREST;
235626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    }
236626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray
237626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    /**
238626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * Retrieve a sampler with min and mag set to linear and wrap modes set to
239626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * mirrored repeat.
240626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
241626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @param rs Context to which the sampler will belong.
242626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
243626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @return Sampler
244626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     */
245626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
246626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
247626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            Builder b = new Builder(rs);
248626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMinification(Value.LINEAR);
249626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMagnification(Value.LINEAR);
250626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapS(Value.MIRRORED_REPEAT);
251626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapT(Value.MIRRORED_REPEAT);
252626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
253626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        }
254626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        return rs.mSampler_MIRRORED_REPEAT_LINEAR;
255626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    }
25698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
25798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
25898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Builder for creating non-standard samplers.  Usefull if mix and match of
25998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * wrap modes is necesary or if anisotropic filtering is desired.
26098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
26198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
26298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static class Builder {
26398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        RenderScript mRS;
26498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mMin;
26598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mMag;
26698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mWrapS;
26798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mWrapT;
26898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mWrapR;
26998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        float mAniso;
27098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
27198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public Builder(RenderScript rs) {
27298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS = rs;
27398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mMin = Value.NEAREST;
27498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mMag = Value.NEAREST;
27598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mWrapS = Value.WRAP;
27698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mWrapT = Value.WRAP;
27798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mWrapR = Value.WRAP;
27898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mAniso = 1.0f;
27998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
28098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
28198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setMinification(Value v) {
28298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (v == Value.NEAREST ||
28398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                v == Value.LINEAR ||
28498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                v == Value.LINEAR_MIP_LINEAR ||
28598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                v == Value.LINEAR_MIP_NEAREST) {
28698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mMin = v;
28798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
28898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
28998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
29098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
29198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
29298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setMagnification(Value v) {
29398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (v == Value.NEAREST || v == Value.LINEAR) {
29498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mMag = v;
29598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
29698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
29798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
29898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
29998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
30098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setWrapS(Value v) {
301626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
30298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mWrapS = v;
30398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
30498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
30598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
30698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
30798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
30898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setWrapT(Value v) {
309626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
31098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mWrapT = v;
31198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
31298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
31398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
31498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
31598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
31698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setAnisotropy(float v) {
31798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if(v >= 0.0f) {
31898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mAniso = v;
31998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
32098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
32198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
32298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
32398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
32498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public Sampler create() {
325ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            if (mRS.isNative) {
326ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                RenderScriptThunker rst = (RenderScriptThunker)mRS;
327ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                SamplerThunker.Builder b = new SamplerThunker.Builder(rst);
328ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setMinification(mMin);
329ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setMagnification(mMag);
330ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setWrapS(mWrapS);
331ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setWrapT(mWrapT);
332ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setAnisotropy(mAniso);
333ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                return b.create();
334ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            }
33598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS.validate();
33698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            int id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
33798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                        mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
33898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Sampler sampler = new Sampler(id, mRS);
33998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mMin = mMin;
34098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mMag = mMag;
34198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mWrapS = mWrapS;
34298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mWrapT = mWrapT;
34398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mWrapR = mWrapR;
34498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mAniso = mAniso;
34598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return sampler;
34698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
34798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
34898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
34998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams}
35098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
351