Texture2D.java revision 1168868e766f0696c30f644292a5a833ee3983e4
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.io.File; 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.FileInputStream; 23import java.io.BufferedInputStream; 24import java.lang.Math; 25import java.net.URL; 26import java.util.ArrayList; 27 28import android.content.res.Resources; 29import android.graphics.Bitmap; 30import android.graphics.BitmapFactory; 31import android.renderscript.*; 32import android.renderscript.Allocation.MipmapControl; 33import android.renderscript.Matrix4f; 34import android.renderscript.Type.Builder; 35import android.util.Log; 36 37/** 38 * @hide 39 */ 40public class Texture2D extends SceneGraphBase { 41 private boolean mLoadFromSD; 42 43 String mFileName; 44 String mFileDir; 45 Allocation mRsTexture; 46 47 public Texture2D() { 48 mLoadFromSD = false; 49 } 50 51 public void setFileDir(String dir) { 52 if (dir.indexOf("sdcard/") != -1) { 53 mLoadFromSD = true; 54 } 55 mFileDir = dir; 56 } 57 58 public void setFileName(String file) { 59 mFileName = file; 60 } 61 62 public String getFileName() { 63 return mFileName; 64 } 65 66 Allocation getRsData(RenderScriptGL rs, Resources res) { 67 if (mRsTexture != null) { 68 return mRsTexture; 69 } 70 71 String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1); 72 InputStream is = null; 73 try { 74 if (!mLoadFromSD) { 75 is = res.getAssets().open(mFileDir + shortName); 76 } else { 77 File f = new File(mFileDir + shortName); 78 is = new BufferedInputStream(new FileInputStream(f)); 79 } 80 } catch (IOException e) { 81 Log.e("Texture2D", 82 "Could not open image file " + shortName + " Message: " + e.getMessage()); 83 return null; 84 } 85 86 Bitmap b = BitmapFactory.decodeStream(is); 87 mRsTexture = Allocation.createFromBitmap(rs, b, 88 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 89 Allocation.USAGE_GRAPHICS_TEXTURE); 90 return mRsTexture; 91 } 92} 93 94 95 96 97 98