Texture2D.java revision a7a211b8a68a7d3f5ff4409aa286db07f96c0550
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 static String mSDCardPath = "sdcard/scenegraph/"; 42 private final boolean mLoadFromSD = true; 43 44 String mFileName; 45 Allocation mRsTexture; 46 47 public Texture2D() { 48 } 49 50 public void setFileName(String file) { 51 mFileName = file; 52 } 53 54 public String getFileName() { 55 return mFileName; 56 } 57 58 Allocation getRsData(RenderScriptGL rs, Resources res) { 59 if (mRsTexture != null) { 60 return mRsTexture; 61 } 62 63 String shortName = mFileName.substring(mFileName.lastIndexOf('/') + 1); 64 InputStream is = null; 65 try { 66 if (!mLoadFromSD) { 67 is = res.getAssets().open(shortName); 68 } else { 69 File f = new File(mSDCardPath + shortName); 70 is = new BufferedInputStream(new FileInputStream(f)); 71 } 72 } catch (IOException e) { 73 Log.e("Texture2D", 74 "Could not open image file " + shortName + " Message: " + e.getMessage()); 75 return null; 76 } 77 78 Bitmap b = BitmapFactory.decodeStream(is); 79 mRsTexture = Allocation.createFromBitmap(rs, b, 80 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 81 Allocation.USAGE_GRAPHICS_TEXTURE); 82 return mRsTexture; 83 } 84} 85 86 87 88 89 90