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/**
317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * Sampler object that defines how Allocations can be read as textures within a
327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * kernel. Samplers are used in conjunction with the {@code rsSample} runtime
337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * function to return values from normalized coordinates.
347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines *
357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * Any Allocation used with a Sampler must have been created with {@link
3660c5b31f4448410221de043873b94797732afa66Stephen Hines * android.support.v8.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE}; using a
3760c5b31f4448410221de043873b94797732afa66Stephen Hines * Sampler on an {@link android.support.v8.renderscript.Allocation} that was not
3860c5b31f4448410221de043873b94797732afa66Stephen Hines * created with
3960c5b31f4448410221de043873b94797732afa66Stephen Hines * {@link android.support.v8.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE} is
4060c5b31f4448410221de043873b94797732afa66Stephen Hines * undefined.
4198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/
4298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class Sampler extends BaseObj {
4398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public enum Value {
4498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        NEAREST (0),
4598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LINEAR (1),
4698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LINEAR_MIP_LINEAR (2),
4798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LINEAR_MIP_NEAREST (5),
4898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        WRAP (3),
49626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        CLAMP (4),
50626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        MIRRORED_REPEAT (6);
5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
5298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int mID;
5398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value(int id) {
5498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mID = id;
5598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
5698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
5798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
5898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mMin;
5998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mMag;
6098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mWrapS;
6198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mWrapT;
6298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Value mWrapR;
6398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    float mAniso;
6498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler(int id, RenderScript rs) {
6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        super(id, rs);
6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return minification setting for the sampler
7198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
7298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getMinification() {
7398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mMin;
7498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
7598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
7698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return magnification setting for the sampler
7898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
7998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getMagnification() {
8098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mMag;
8198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
8298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return S wrapping mode for the sampler
8598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
8698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getWrapS() {
8798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mWrapS;
8898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
8998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
9098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return T wrapping mode for the sampler
9298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
9398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Value getWrapT() {
9498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mWrapT;
9598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
9698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
9798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return anisotropy setting for the sampler
9998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
10098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public float getAnisotropy() {
10198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mAniso;
10298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
10398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
10498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
10598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Retrieve a sampler with min and mag set to nearest and wrap modes set to
10698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * clamp.
10798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
10898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
10998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
11098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
11198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
11298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler CLAMP_NEAREST(RenderScript rs) {
11398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_CLAMP_NEAREST == null) {
11498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
11598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.NEAREST);
11698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.NEAREST);
11798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.CLAMP);
11898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.CLAMP);
11998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_CLAMP_NEAREST = b.create();
12098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
12198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_CLAMP_NEAREST;
12298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
12398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
12498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Retrieve a sampler with min and mag set to linear and wrap modes set to
12698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * clamp.
12798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
12998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
13098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
13198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
13298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler CLAMP_LINEAR(RenderScript rs) {
13398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_CLAMP_LINEAR == null) {
13498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
13598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR);
13698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
13798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.CLAMP);
13898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.CLAMP);
13998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_CLAMP_LINEAR = b.create();
14098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
14198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_CLAMP_LINEAR;
14298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
14398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
14498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
145f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
146626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * wrap modes set to clamp.
14798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
14898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
14998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
15098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
15198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
15298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
15398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
15498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
15598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR_MIP_LINEAR);
15698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
15798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.CLAMP);
15898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.CLAMP);
15998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
16098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
16198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
16298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
16398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
16498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
16598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Retrieve a sampler with min and mag set to nearest and wrap modes set to
16698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * wrap.
16798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
16898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
16998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
17098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
17198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
17298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler WRAP_NEAREST(RenderScript rs) {
17398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_WRAP_NEAREST == null) {
17498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
17598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.NEAREST);
17698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.NEAREST);
17798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.WRAP);
17898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.WRAP);
17998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_WRAP_NEAREST = b.create();
18098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
18198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_WRAP_NEAREST;
18298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
18398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
18498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
185f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * Retrieve a sampler with min and mag set to linear and wrap modes set to
18698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * wrap.
18798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
18898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
18998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
19098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
19198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
19298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler WRAP_LINEAR(RenderScript rs) {
19398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_WRAP_LINEAR == null) {
19498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
19598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR);
19698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
19798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.WRAP);
19898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.WRAP);
19998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_WRAP_LINEAR = b.create();
20098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
20198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_WRAP_LINEAR;
20298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
20398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
20498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
205f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
206626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * wrap modes set to wrap.
20798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
20898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the sampler will belong.
20998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
21098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Sampler
21198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
21298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
21398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
21498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Builder b = new Builder(rs);
21598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMinification(Value.LINEAR_MIP_LINEAR);
21698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setMagnification(Value.LINEAR);
21798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapS(Value.WRAP);
21898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            b.setWrapT(Value.WRAP);
21998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
22098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
22198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
22298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
22398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
224626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    /**
225626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * Retrieve a sampler with min and mag set to nearest and wrap modes set to
226626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * mirrored repeat.
227626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
228626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @param rs Context to which the sampler will belong.
229626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
230626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @return Sampler
231626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     */
232626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
233626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
234626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            Builder b = new Builder(rs);
235626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMinification(Value.NEAREST);
236626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMagnification(Value.NEAREST);
237626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapS(Value.MIRRORED_REPEAT);
238626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapT(Value.MIRRORED_REPEAT);
239626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
240626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        }
241626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        return rs.mSampler_MIRRORED_REPEAT_NEAREST;
242626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    }
243626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray
244626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    /**
245626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * Retrieve a sampler with min and mag set to linear and wrap modes set to
246626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * mirrored repeat.
247626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
248626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @param rs Context to which the sampler will belong.
249626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     *
250626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     * @return Sampler
251626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray     */
252626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
253626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
254626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            Builder b = new Builder(rs);
255626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMinification(Value.LINEAR);
256626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setMagnification(Value.LINEAR);
257626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapS(Value.MIRRORED_REPEAT);
258626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            b.setWrapT(Value.MIRRORED_REPEAT);
259626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
260626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        }
261626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray        return rs.mSampler_MIRRORED_REPEAT_LINEAR;
262626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    }
26398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
26498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
2657d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Builder for creating non-standard samplers.  This is only necessary if
2667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * a Sampler with different min and mag modes is desired.
26798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
26898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static class Builder {
26998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        RenderScript mRS;
27098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mMin;
27198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mMag;
27298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mWrapS;
27398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mWrapT;
27498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Value mWrapR;
27598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        float mAniso;
27698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
27798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public Builder(RenderScript rs) {
27898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS = rs;
27998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mMin = Value.NEAREST;
28098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mMag = Value.NEAREST;
28198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mWrapS = Value.WRAP;
28298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mWrapT = Value.WRAP;
28398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mWrapR = Value.WRAP;
28498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mAniso = 1.0f;
28598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
28698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
28798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setMinification(Value v) {
28898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (v == Value.NEAREST ||
28998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                v == Value.LINEAR ||
29098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                v == Value.LINEAR_MIP_LINEAR ||
29198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                v == Value.LINEAR_MIP_NEAREST) {
29298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mMin = v;
29398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
29498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
29598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
29698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
29798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
29898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setMagnification(Value v) {
29998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (v == Value.NEAREST || v == Value.LINEAR) {
30098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mMag = v;
30198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
30298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
30398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
30498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
30598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
30698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setWrapS(Value v) {
307626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
30898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mWrapS = v;
30998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
31098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
31198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
31298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
31398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
31498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setWrapT(Value v) {
315626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray            if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
31698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mWrapT = v;
31798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
31898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
31998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
32098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
32198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
32298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void setAnisotropy(float v) {
32398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if(v >= 0.0f) {
32498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mAniso = v;
32598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            } else {
32698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new IllegalArgumentException("Invalid value");
32798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
32898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
32998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
33098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public Sampler create() {
331ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            if (mRS.isNative) {
332ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                RenderScriptThunker rst = (RenderScriptThunker)mRS;
333ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                SamplerThunker.Builder b = new SamplerThunker.Builder(rst);
334ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setMinification(mMin);
335ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setMagnification(mMag);
336ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setWrapS(mWrapS);
337ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setWrapT(mWrapT);
338ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                b.setAnisotropy(mAniso);
339ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                return b.create();
340ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            }
34198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS.validate();
34298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            int id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
34398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                        mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
34498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            Sampler sampler = new Sampler(id, mRS);
34598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mMin = mMin;
34698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mMag = mMag;
34798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mWrapS = mWrapS;
34898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mWrapT = mWrapT;
34998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mWrapR = mWrapR;
35098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            sampler.mAniso = mAniso;
35198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return sampler;
35298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
35398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
35498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
35598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams}
35698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
357