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 17 package androidx.media.filterfw; 18 19 import android.graphics.Bitmap; 20 import android.opengl.GLES11Ext; 21 import android.opengl.GLES20; 22 23 import java.nio.ByteBuffer; 24 25 public class TextureSource { 26 27 private int mTexId; 28 private int mTarget; 29 private boolean mIsOwner; 30 private boolean mIsAllocated = false; 31 fromTexture(int texId, int target)32 public static TextureSource fromTexture(int texId, int target) { 33 return new TextureSource(texId, target, false); 34 } 35 fromTexture(int texId)36 public static TextureSource fromTexture(int texId) { 37 return new TextureSource(texId, GLES20.GL_TEXTURE_2D, false); 38 } 39 newTexture()40 public static TextureSource newTexture() { 41 return new TextureSource(GLToolbox.generateTexture(), GLES20.GL_TEXTURE_2D, true); 42 } 43 newExternalTexture()44 public static TextureSource newExternalTexture() { 45 return new TextureSource(GLToolbox.generateTexture(), 46 GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 47 true); 48 } 49 getTextureId()50 public int getTextureId() { 51 return mTexId; 52 } 53 getTarget()54 public int getTarget() { 55 return mTarget; 56 } 57 bind()58 public void bind() { 59 GLES20.glBindTexture(mTarget, mTexId); 60 GLToolbox.checkGlError("glBindTexture"); 61 } 62 allocate(int width, int height)63 public void allocate(int width, int height) { 64 //Log.i("TextureSource", "Allocating empty texture " + mTexId + ": " + width + "x" + height + "."); 65 GLToolbox.allocateTexturePixels(mTexId, mTarget, width, height); 66 mIsAllocated = true; 67 } 68 allocateWithPixels(ByteBuffer pixels, int width, int height)69 public void allocateWithPixels(ByteBuffer pixels, int width, int height) { 70 //Log.i("TextureSource", "Uploading pixels to texture " + mTexId + ": " + width + "x" + height + "."); 71 GLToolbox.setTexturePixels(mTexId, mTarget, pixels, width, height); 72 mIsAllocated = true; 73 } 74 allocateWithBitmapPixels(Bitmap bitmap)75 public void allocateWithBitmapPixels(Bitmap bitmap) { 76 //Log.i("TextureSource", "Uploading pixels to texture " + mTexId + "!"); 77 GLToolbox.setTexturePixels(mTexId, mTarget, bitmap); 78 mIsAllocated = true; 79 } 80 generateMipmaps()81 public void generateMipmaps() { 82 GLES20.glBindTexture(mTarget, mTexId); 83 GLES20.glTexParameteri(mTarget, 84 GLES20.GL_TEXTURE_MIN_FILTER, 85 GLES20.GL_LINEAR_MIPMAP_LINEAR); 86 GLES20.glGenerateMipmap(mTarget); 87 GLES20.glBindTexture(mTarget, 0); 88 } 89 setParameter(int parameter, int value)90 public void setParameter(int parameter, int value) { 91 GLES20.glBindTexture(mTarget, mTexId); 92 GLES20.glTexParameteri(mTarget, parameter, value); 93 GLES20.glBindTexture(mTarget, 0); 94 } 95 96 /** 97 * @hide 98 */ release()99 public void release() { 100 if (GLToolbox.isTexture(mTexId) && mIsOwner) { 101 GLToolbox.deleteTexture(mTexId); 102 } 103 mTexId = GLToolbox.textureNone(); 104 } 105 106 @Override toString()107 public String toString() { 108 return "TextureSource(id=" + mTexId + ", target=" + mTarget + ")"; 109 } 110 isAllocated()111 boolean isAllocated() { 112 return mIsAllocated; 113 } 114 TextureSource(int texId, int target, boolean isOwner)115 private TextureSource(int texId, int target, boolean isOwner) { 116 mTexId = texId; 117 mTarget = target; 118 mIsOwner = isOwner; 119 } 120 } 121 122