1/*
2 * Copyright (C) 2011 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
17package com.android.scenegraph;
18
19import java.lang.Math;
20
21import com.android.scenegraph.SceneManager;
22
23import android.content.res.Resources;
24import android.renderscript.*;
25import android.util.Log;
26
27/**
28 * @hide
29 */
30public class Texture2D extends TextureBase {
31    String mFileName;
32    String mFileDir;
33    int mResourceID;
34
35    public Texture2D() {
36        super(ScriptC_export.const_TextureType_TEXTURE_2D);
37    }
38
39    public Texture2D(Allocation tex) {
40        super(ScriptC_export.const_TextureType_TEXTURE_2D);
41        setTexture(tex);
42    }
43
44    public Texture2D(String dir, String file) {
45        super(ScriptC_export.const_TextureType_TEXTURE_CUBE);
46        setFileDir(dir);
47        setFileName(file);
48    }
49
50    public Texture2D(int resourceID) {
51        super(ScriptC_export.const_TextureType_TEXTURE_2D);
52        mResourceID = resourceID;
53    }
54
55    public void setFileDir(String dir) {
56        mFileDir = dir;
57    }
58
59    public void setFileName(String file) {
60        mFileName = file;
61    }
62
63    public String getFileName() {
64        return mFileName;
65    }
66
67    public void setTexture(Allocation tex) {
68        mData.texture = tex != null ? tex : SceneManager.getDefaultTex2D();
69        if (mField != null) {
70            mField.set_texture(0, mData.texture, true);
71        }
72    }
73
74    void load() {
75        RenderScriptGL rs = SceneManager.getRS();
76        Resources res = SceneManager.getRes();
77        if (mFileName != null && mFileName.length() > 0) {
78            String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1);
79            setTexture(SceneManager.loadTexture2D(mFileDir + shortName, rs, res));
80        } else if (mResourceID != 0) {
81            setTexture(SceneManager.loadTexture2D(mResourceID, rs, res));
82        }
83    }
84
85    ScriptField_Texture_s getRsData(boolean loadNow) {
86        if (mField != null) {
87            return mField;
88        }
89
90        RenderScriptGL rs = SceneManager.getRS();
91        Resources res = SceneManager.getRes();
92        if (rs == null || res == null) {
93            return null;
94        }
95
96        mField = new ScriptField_Texture_s(rs, 1);
97
98        if (loadNow) {
99            load();
100        } else {
101            mData.texture = SceneManager.getDefaultTex2D();
102            new SingleImageLoaderTask().execute(this);
103        }
104
105        mField.set(mData, 0, true);
106        return mField;
107    }
108}
109
110
111
112
113
114