Home | History | Annotate | Download | only in glrenderer
      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.glrenderer;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.Bitmap.Config;
     21 import android.test.suitebuilder.annotation.SmallTest;
     22 
     23 import com.android.gallery3d.glrenderer.BasicTexture;
     24 import com.android.gallery3d.glrenderer.BitmapTexture;
     25 import com.android.gallery3d.glrenderer.ColorTexture;
     26 import com.android.gallery3d.glrenderer.GLCanvas;
     27 import com.android.gallery3d.glrenderer.GLES11Canvas;
     28 import com.android.gallery3d.glrenderer.UploadedTexture;
     29 
     30 import junit.framework.TestCase;
     31 
     32 import javax.microedition.khronos.opengles.GL11;
     33 
     34 @SmallTest
     35 public class TextureTest extends TestCase {
     36     @SuppressWarnings("unused")
     37     private static final String TAG = "TextureTest";
     38 
     39     class MyBasicTexture extends BasicTexture {
     40         int mOnBindCalled;
     41         int mOpaqueCalled;
     42 
     43         MyBasicTexture(GLCanvas canvas, int id) {
     44             super(canvas, id, 0);
     45         }
     46 
     47         @Override
     48         protected boolean onBind(GLCanvas canvas) {
     49             mOnBindCalled++;
     50             return true;
     51         }
     52 
     53         @Override
     54         protected int getTarget() {
     55             return GL11.GL_TEXTURE_2D;
     56         }
     57 
     58         @Override
     59         public boolean isOpaque() {
     60             mOpaqueCalled++;
     61             return true;
     62         }
     63 
     64         void upload() {
     65             mState = STATE_LOADED;
     66         }
     67     }
     68 
     69     @SmallTest
     70     public void testBasicTexture() {
     71         GL11 glStub = new GLStub();
     72         GLCanvas canvas = new GLES11Canvas(glStub);
     73         MyBasicTexture texture = new MyBasicTexture(canvas, 47);
     74 
     75         assertEquals(47, texture.getId());
     76         texture.setSize(1, 1);
     77         assertEquals(1, texture.getWidth());
     78         assertEquals(1, texture.getHeight());
     79         assertEquals(1, texture.getTextureWidth());
     80         assertEquals(1, texture.getTextureHeight());
     81         texture.setSize(3, 5);
     82         assertEquals(3, texture.getWidth());
     83         assertEquals(5, texture.getHeight());
     84         assertEquals(4, texture.getTextureWidth());
     85         assertEquals(8, texture.getTextureHeight());
     86 
     87         assertFalse(texture.isLoaded());
     88         texture.upload();
     89         assertTrue(texture.isLoaded());
     90 
     91         // For a different GL, it's not loaded.
     92         GLCanvas canvas2 = new GLES11Canvas(glStub);
     93         assertFalse(texture.isLoaded());
     94 
     95         assertEquals(0, texture.mOnBindCalled);
     96         assertEquals(0, texture.mOpaqueCalled);
     97         texture.draw(canvas, 100, 200, 1, 1);
     98         assertEquals(1, texture.mOnBindCalled);
     99         assertEquals(1, texture.mOpaqueCalled);
    100         texture.draw(canvas, 0, 0);
    101         assertEquals(2, texture.mOnBindCalled);
    102         assertEquals(2, texture.mOpaqueCalled);
    103     }
    104 
    105     @SmallTest
    106     public void testColorTexture() {
    107         GLCanvasMock canvas = new GLCanvasMock();
    108         ColorTexture texture = new ColorTexture(0x12345678);
    109 
    110         texture.setSize(42, 47);
    111         assertEquals(texture.getWidth(), 42);
    112         assertEquals(texture.getHeight(), 47);
    113         assertEquals(0, canvas.mFillRectCalled);
    114         texture.draw(canvas, 0, 0);
    115         assertEquals(1, canvas.mFillRectCalled);
    116         assertEquals(0x12345678, canvas.mFillRectColor);
    117         assertEquals(42f, canvas.mFillRectWidth);
    118         assertEquals(47f, canvas.mFillRectHeight);
    119         assertFalse(texture.isOpaque());
    120         assertTrue(new ColorTexture(0xFF000000).isOpaque());
    121     }
    122 
    123     private class MyUploadedTexture extends UploadedTexture {
    124         int mGetCalled;
    125         int mFreeCalled;
    126         Bitmap mBitmap;
    127         @Override
    128         protected Bitmap onGetBitmap() {
    129             mGetCalled++;
    130             Config config = Config.ARGB_8888;
    131             mBitmap = Bitmap.createBitmap(47, 42, config);
    132             return mBitmap;
    133         }
    134         @Override
    135         protected void onFreeBitmap(Bitmap bitmap) {
    136             mFreeCalled++;
    137             assertSame(mBitmap, bitmap);
    138             mBitmap.recycle();
    139             mBitmap = null;
    140         }
    141     }
    142 
    143     @SmallTest
    144     public void testUploadedTexture() {
    145         GL11 glStub = new GLStub();
    146         GLCanvas canvas = new GLES11Canvas(glStub);
    147         MyUploadedTexture texture = new MyUploadedTexture();
    148 
    149         // draw it and the bitmap should be fetched.
    150         assertEquals(0, texture.mFreeCalled);
    151         assertEquals(0, texture.mGetCalled);
    152         texture.draw(canvas, 0, 0);
    153         assertEquals(1, texture.mGetCalled);
    154         assertTrue(texture.isLoaded());
    155         assertTrue(texture.isContentValid());
    156 
    157         // invalidate content and it should be freed.
    158         texture.invalidateContent();
    159         assertFalse(texture.isContentValid());
    160         assertEquals(1, texture.mFreeCalled);
    161         assertTrue(texture.isLoaded());  // But it's still loaded
    162 
    163         // draw it again and the bitmap should be fetched again.
    164         texture.draw(canvas, 0, 0);
    165         assertEquals(2, texture.mGetCalled);
    166         assertTrue(texture.isLoaded());
    167         assertTrue(texture.isContentValid());
    168 
    169         // recycle the texture and it should be freed again.
    170         texture.recycle();
    171         assertEquals(2, texture.mFreeCalled);
    172         // TODO: these two are broken and waiting for fix.
    173         //assertFalse(texture.isLoaded(canvas));
    174         //assertFalse(texture.isContentValid(canvas));
    175     }
    176 
    177     class MyTextureForMixed extends BasicTexture {
    178         MyTextureForMixed(GLCanvas canvas, int id) {
    179             super(canvas, id, 0);
    180         }
    181 
    182         @Override
    183         protected boolean onBind(GLCanvas canvas) {
    184             return true;
    185         }
    186 
    187         @Override
    188         protected int getTarget() {
    189             return GL11.GL_TEXTURE_2D;
    190         }
    191 
    192         @Override
    193         public boolean isOpaque() {
    194             return true;
    195         }
    196     }
    197 
    198     @SmallTest
    199     public void testBitmapTexture() {
    200         Config config = Config.ARGB_8888;
    201         Bitmap bitmap = Bitmap.createBitmap(47, 42, config);
    202         assertFalse(bitmap.isRecycled());
    203         BitmapTexture texture = new BitmapTexture(bitmap);
    204         texture.recycle();
    205         assertFalse(bitmap.isRecycled());
    206         bitmap.recycle();
    207         assertTrue(bitmap.isRecycled());
    208     }
    209 }
    210