1 /* 2 * Copyright (C) 2012 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 com.android.gallery3d.ui; 18 19 import javax.microedition.khronos.opengles.GL11; 20 import javax.microedition.khronos.opengles.GL11Ext; 21 22 // ExtTexture is a texture whose content comes from a external texture. 23 // Before drawing, setSize() should be called. 24 public class ExtTexture extends BasicTexture { 25 26 private static int[] sTextureId = new int[1]; 27 private static float[] sCropRect = new float[4]; 28 private int mTarget; 29 30 public ExtTexture(int target) { 31 GLId.glGenTextures(1, sTextureId, 0); 32 mId = sTextureId[0]; 33 mTarget = target; 34 } 35 36 private void uploadToCanvas(GLCanvas canvas) { 37 GL11 gl = canvas.getGLInstance(); 38 39 int width = getWidth(); 40 int height = getHeight(); 41 // Define a vertically flipped crop rectangle for OES_draw_texture. 42 // The four values in sCropRect are: left, bottom, width, and 43 // height. Negative value of width or height means flip. 44 sCropRect[0] = 0; 45 sCropRect[1] = height; 46 sCropRect[2] = width; 47 sCropRect[3] = -height; 48 49 // Set texture parameters. 50 gl.glBindTexture(mTarget, mId); 51 gl.glTexParameterfv(mTarget, 52 GL11Ext.GL_TEXTURE_CROP_RECT_OES, sCropRect, 0); 53 gl.glTexParameteri(mTarget, 54 GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP_TO_EDGE); 55 gl.glTexParameteri(mTarget, 56 GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP_TO_EDGE); 57 gl.glTexParameterf(mTarget, 58 GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); 59 gl.glTexParameterf(mTarget, 60 GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); 61 62 setAssociatedCanvas(canvas); 63 mState = STATE_LOADED; 64 } 65 66 @Override 67 protected boolean onBind(GLCanvas canvas) { 68 if (!isLoaded()) { 69 uploadToCanvas(canvas); 70 } 71 72 return true; 73 } 74 75 @Override 76 public int getTarget() { 77 return mTarget; 78 } 79 80 @Override 81 public boolean isOpaque() { 82 return true; 83 } 84 85 @Override 86 public void yield() { 87 // we cannot free the texture because we have no backup. 88 } 89 } 90