Android Open Source - fun-gl Texture Manager






From Project

Back to project page fun-gl.

License

The source code is released under:

Apache License

If you think the Android project fun-gl listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jcxavier.android.opengl.engine.texture;
/*w ww.j  a  v a  2  s .  co m*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;
import com.jcxavier.android.opengl.file.FileManager;
import com.jcxavier.android.opengl.util.BitmapUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static android.opengl.GLES20.*;

/**
 * Created on 12/03/2014.
 *
 * @author Joo Xavier <jcxavier@jcxavier.com>
 */
public final class TextureManager {

    private static final String TAG = TextureManager.class.getSimpleName();

    private static TextureManager sTextureManager = null;

    private final Map<String, Texture> mTextures;
    private int mMaxTextureSize;

    private TextureManager() {
        mTextures = new HashMap<>();
        mMaxTextureSize = 0;
    }

    public static TextureManager getInstance() {
        if (sTextureManager == null) {
            sTextureManager = new TextureManager();
        }

        return sTextureManager;
    }

    public void clean() {
        Collection<Texture> textures = mTextures.values();

        if (textures.isEmpty()) {
            return;
        }

        int[] buffer = new int[textures.size()];
        int idx = 0;

        for (Texture texture : textures) {
            buffer[idx] = texture.getName();
            idx++;
        }

        // remove information from GL
        glDeleteTextures(buffer.length, buffer, 0);

        mTextures.clear();
    }

    public void readMaxTextureSize() {
        int[] buffer = { 0 };
        glGetIntegerv(GL_MAX_TEXTURE_SIZE, buffer, 0);
        mMaxTextureSize = buffer[0];
    }

    public void preload(final String imageName) {
        load(imageName);
    }

    public Texture load(final String imageName) {
        if (mMaxTextureSize == 0) {
            throw new IllegalStateException("GL_MAX_TEXTURE_SIZE must be read before attempting to load textures.");
        }

        Texture texture = mTextures.get(imageName);

        if (texture == null) {
            // texture is not yet cached, needs to be loaded onto memory
            Bitmap image = readBitmap(imageName);
            int textureName = createTexture();
            texture = loadTextureToGL(image, textureName);

            if (texture != null) {
                // cache the texture if it is valid
                mTextures.put(imageName, texture);

                Log.d(TAG, String.format("Loaded texture %s: %s", imageName, texture));
            }
        }

        return texture;
    }

    private Bitmap readBitmap(final String imageName) {
        try {
            // load the input stream into a byte array
            InputStream is = FileManager.getInstance().readFile(imageName);
            byte[] imageData = BitmapUtils.readInputStreamAsByteArray(is);
            is.close();

            // try to load the bitmap in the original size
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 1;

            // read out size
            BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

            while (options.outHeight > mMaxTextureSize || options.outWidth > mMaxTextureSize) {
                // retry, with 1/2 of the size of the texture
                options.inSampleSize *= 2;
                BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
            }

            // now decode the actual bitmap
            options.inJustDecodeBounds = false;

            return BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        } catch (IOException ioe) {
            Log.e(TAG, String.format("Texture %s couldn't be read! %s", imageName, ioe.getMessage()));
        }

        return null;
    }

    private static int createTexture() {
        // generate a new OpenGL texture name and bind to it for sending the pixel data to OpenGL
        int[] buffer = { 0 };
        glGenTextures(1, buffer, 0);
        glBindTexture(GL_TEXTURE_2D, buffer[0]);

        return buffer[0];
    }

    private static Texture loadTextureToGL(final Bitmap image, final int textureName) {
        if (image == null || textureName == 0) {
            // there was a problem either in loading the image or generating the texture name
            return null;
        }

        // read image information
        int width = image.getWidth();
        int height = image.getHeight();
        boolean premultipliedAlpha = image.getConfig() != Bitmap.Config.RGB_565 && image.hasAlpha();

        Texture texture = new Texture(textureName, width, height, premultipliedAlpha);
        texture.configureTextureParameters();

        // load image
        GLUtils.texImage2D(GL_TEXTURE_2D, 0, image, 0);

        // check for GL error
        int glError = glGetError();

        // recycle bitmap, it won't be used again
        image.recycle();

        if (glError != GL_NO_ERROR) {
            Log.e(TAG, "Error loading bitmap: " + glError);
            // keep going, this might not be 100% fatal
        }

        return texture;
    }
}




Java Source Code List

com.jcxavier.android.opengl.engine.BitmapConfigHelper.java
com.jcxavier.android.opengl.engine.EngineActivity.java
com.jcxavier.android.opengl.engine.EngineRenderer.java
com.jcxavier.android.opengl.engine.EngineView.java
com.jcxavier.android.opengl.engine.RendererOptions.java
com.jcxavier.android.opengl.engine.cache.GLState.java
com.jcxavier.android.opengl.engine.gdx.GdxEglConfigChooser.java
com.jcxavier.android.opengl.engine.shader.ColorShader.java
com.jcxavier.android.opengl.engine.shader.ShaderManager.java
com.jcxavier.android.opengl.engine.shader.Shader.java
com.jcxavier.android.opengl.engine.shader.TextureShader.java
com.jcxavier.android.opengl.engine.texture.TextureFilteringMode.java
com.jcxavier.android.opengl.engine.texture.TextureManager.java
com.jcxavier.android.opengl.engine.texture.TextureWrap.java
com.jcxavier.android.opengl.engine.texture.Texture.java
com.jcxavier.android.opengl.engine.type.RotationMode.java
com.jcxavier.android.opengl.file.FileManager.java
com.jcxavier.android.opengl.game.GameStage.java
com.jcxavier.android.opengl.game.SimpleGameStage.java
com.jcxavier.android.opengl.game.camera.Camera.java
com.jcxavier.android.opengl.game.camera.DefaultCamera.java
com.jcxavier.android.opengl.game.camera.OrthographicCamera.java
com.jcxavier.android.opengl.game.manager.GameManager.java
com.jcxavier.android.opengl.game.manager.ScreenManager.java
com.jcxavier.android.opengl.game.manager.input.InputHandler.java
com.jcxavier.android.opengl.game.manager.input.InputManager.java
com.jcxavier.android.opengl.game.object.DrawableObject.java
com.jcxavier.android.opengl.game.object.GameObject.java
com.jcxavier.android.opengl.game.object.Sprite.java
com.jcxavier.android.opengl.game.type.Resizeable.java
com.jcxavier.android.opengl.game.type.Touchable.java
com.jcxavier.android.opengl.game.type.Transformable.java
com.jcxavier.android.opengl.game.type.Updateable.java
com.jcxavier.android.opengl.math.IVector.java
com.jcxavier.android.opengl.math.Matrix4.java
com.jcxavier.android.opengl.math.Vector2.java
com.jcxavier.android.opengl.math.Vector3.java
com.jcxavier.android.opengl.math.Vector4.java
com.jcxavier.android.opengl.sample.GameActivity.java
com.jcxavier.android.opengl.sample.TestStage.java
com.jcxavier.android.opengl.util.BitmapUtils.java
com.jcxavier.android.opengl.util.Constants.java
com.jcxavier.android.opengl.util.ReflectionUtils.java
com.jcxavier.android.opengl.util.WeakList.java
com.sample.clean.TestActivity.java
com.sample.clean.TestStage.java