Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 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 public class RawTexture extends BasicTexture {
     23     private static final String TAG = "RawTexture";
     24 
     25     private final static int[] sTextureId = new int[1];
     26     private final static float[] sCropRect = new float[4];
     27 
     28     private final boolean mOpaque;
     29 
     30     public RawTexture(int width, int height, boolean opaque) {
     31         mOpaque = opaque;
     32         setSize(width, height);
     33     }
     34 
     35     @Override
     36     public boolean isOpaque() {
     37         return mOpaque;
     38     }
     39 
     40     protected void prepare(GLCanvas canvas) {
     41         GL11 gl = canvas.getGLInstance();
     42 
     43         // Define a vertically flipped crop rectangle for
     44         // OES_draw_texture.
     45         // The four values in sCropRect are: left, bottom, width, and
     46         // height. Negative value of width or height means flip.
     47         sCropRect[0] = 0;
     48         sCropRect[1] = mHeight;
     49         sCropRect[2] = mWidth;
     50         sCropRect[3] = -mHeight;
     51 
     52         // Upload the bitmap to a new texture.
     53         GLId.glGenTextures(1, sTextureId, 0);
     54         gl.glBindTexture(GL11.GL_TEXTURE_2D, sTextureId[0]);
     55         gl.glTexParameterfv(GL11.GL_TEXTURE_2D,
     56                 GL11Ext.GL_TEXTURE_CROP_RECT_OES, sCropRect, 0);
     57         gl.glTexParameteri(GL11.GL_TEXTURE_2D,
     58                 GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP_TO_EDGE);
     59         gl.glTexParameteri(GL11.GL_TEXTURE_2D,
     60                 GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP_TO_EDGE);
     61         gl.glTexParameterf(GL11.GL_TEXTURE_2D,
     62                 GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
     63         gl.glTexParameterf(GL11.GL_TEXTURE_2D,
     64                 GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
     65 
     66         gl.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA,
     67                 getTextureWidth(), getTextureHeight(),
     68                 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, null);
     69 
     70         mId = sTextureId[0];
     71         mState = STATE_LOADED;
     72         setAssociatedCanvas(canvas);
     73     }
     74 
     75     @Override
     76     protected boolean onBind(GLCanvas canvas) {
     77         if (isLoaded()) return true;
     78         Log.w(TAG, "lost the content due to context change");
     79         return false;
     80     }
     81 
     82     @Override
     83      public void yield() {
     84          // we cannot free the texture because we have no backup.
     85      }
     86 
     87     @Override
     88     protected int getTarget() {
     89         return GL11.GL_TEXTURE_2D;
     90     }
     91 }
     92