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 java.nio.Buffer; 20 import java.util.HashMap; 21 import javax.microedition.khronos.opengles.GL10; 22 import javax.microedition.khronos.opengles.GL11; 23 24 public class GLMock extends GLStub { 25 @SuppressWarnings("unused") 26 private static final String TAG = "GLMock"; 27 28 // glClear 29 int mGLClearCalled; 30 int mGLClearMask; 31 // glBlendFunc 32 int mGLBlendFuncCalled; 33 int mGLBlendFuncSFactor; 34 int mGLBlendFuncDFactor; 35 // glColor4[fx] 36 int mGLColorCalled; 37 int mGLColor; 38 // glEnable, glDisable 39 boolean mGLBlendEnabled; 40 boolean mGLStencilEnabled; 41 // glEnableClientState 42 boolean mGLVertexArrayEnabled; 43 // glVertexPointer 44 PointerInfo mGLVertexPointer; 45 // glMatrixMode 46 int mGLMatrixMode = GL10.GL_MODELVIEW; 47 // glLoadMatrixf 48 float[] mGLModelViewMatrix = new float[16]; 49 float[] mGLProjectionMatrix = new float[16]; 50 // glBindTexture 51 int mGLBindTextureId; 52 // glTexEnvf 53 HashMap<Integer, Float> mGLTexEnv0 = new HashMap<Integer, Float>(); 54 HashMap<Integer, Float> mGLTexEnv1 = new HashMap<Integer, Float>(); 55 // glActiveTexture 56 int mGLActiveTexture = GL11.GL_TEXTURE0; 57 58 @Override glClear(int mask)59 public void glClear(int mask) { 60 mGLClearCalled++; 61 mGLClearMask = mask; 62 } 63 64 @Override glBlendFunc(int sfactor, int dfactor)65 public void glBlendFunc(int sfactor, int dfactor) { 66 mGLBlendFuncSFactor = sfactor; 67 mGLBlendFuncDFactor = dfactor; 68 mGLBlendFuncCalled++; 69 } 70 71 @Override glColor4f(float red, float green, float blue, float alpha)72 public void glColor4f(float red, float green, float blue, 73 float alpha) { 74 mGLColorCalled++; 75 mGLColor = makeColor4f(red, green, blue, alpha); 76 } 77 78 @Override glColor4x(int red, int green, int blue, int alpha)79 public void glColor4x(int red, int green, int blue, int alpha) { 80 mGLColorCalled++; 81 mGLColor = makeColor4x(red, green, blue, alpha); 82 } 83 84 @Override glEnable(int cap)85 public void glEnable(int cap) { 86 if (cap == GL11.GL_BLEND) { 87 mGLBlendEnabled = true; 88 } else if (cap == GL11.GL_STENCIL_TEST) { 89 mGLStencilEnabled = true; 90 } 91 } 92 93 @Override glDisable(int cap)94 public void glDisable(int cap) { 95 if (cap == GL11.GL_BLEND) { 96 mGLBlendEnabled = false; 97 } else if (cap == GL11.GL_STENCIL_TEST) { 98 mGLStencilEnabled = false; 99 } 100 } 101 102 @Override glEnableClientState(int array)103 public void glEnableClientState(int array) { 104 if (array == GL10.GL_VERTEX_ARRAY) { 105 mGLVertexArrayEnabled = true; 106 } 107 } 108 109 @Override glVertexPointer(int size, int type, int stride, Buffer pointer)110 public void glVertexPointer(int size, int type, int stride, Buffer pointer) { 111 mGLVertexPointer = new PointerInfo(size, type, stride, pointer); 112 } 113 114 @Override glMatrixMode(int mode)115 public void glMatrixMode(int mode) { 116 mGLMatrixMode = mode; 117 } 118 119 @Override glLoadMatrixf(float[] m, int offset)120 public void glLoadMatrixf(float[] m, int offset) { 121 if (mGLMatrixMode == GL10.GL_MODELVIEW) { 122 System.arraycopy(m, offset, mGLModelViewMatrix, 0, 16); 123 } else if (mGLMatrixMode == GL10.GL_PROJECTION) { 124 System.arraycopy(m, offset, mGLProjectionMatrix, 0, 16); 125 } 126 } 127 128 @Override glOrthof( float left, float right, float bottom, float top, float zNear, float zFar)129 public void glOrthof( 130 float left, float right, float bottom, float top, 131 float zNear, float zFar) { 132 float tx = -(right + left) / (right - left); 133 float ty = -(top + bottom) / (top - bottom); 134 float tz = - (zFar + zNear) / (zFar - zNear); 135 float[] m = new float[] { 136 2 / (right - left), 0, 0, 0, 137 0, 2 / (top - bottom), 0, 0, 138 0, 0, -2 / (zFar - zNear), 0, 139 tx, ty, tz, 1 140 }; 141 glLoadMatrixf(m, 0); 142 } 143 144 @Override glBindTexture(int target, int texture)145 public void glBindTexture(int target, int texture) { 146 if (target == GL11.GL_TEXTURE_2D) { 147 mGLBindTextureId = texture; 148 } 149 } 150 151 @Override glTexEnvf(int target, int pname, float param)152 public void glTexEnvf(int target, int pname, float param) { 153 if (target == GL11.GL_TEXTURE_ENV) { 154 if (mGLActiveTexture == GL11.GL_TEXTURE0) { 155 mGLTexEnv0.put(pname, param); 156 } else if (mGLActiveTexture == GL11.GL_TEXTURE1) { 157 mGLTexEnv1.put(pname, param); 158 } else { 159 throw new AssertionError(); 160 } 161 } 162 } 163 getTexEnvi(int pname)164 public int getTexEnvi(int pname) { 165 return getTexEnvi(mGLActiveTexture, pname); 166 } 167 getTexEnvi(int activeTexture, int pname)168 public int getTexEnvi(int activeTexture, int pname) { 169 if (activeTexture == GL11.GL_TEXTURE0) { 170 return (int) mGLTexEnv0.get(pname).floatValue(); 171 } else if (activeTexture == GL11.GL_TEXTURE1) { 172 return (int) mGLTexEnv1.get(pname).floatValue(); 173 } else { 174 throw new AssertionError(); 175 } 176 } 177 178 @Override glActiveTexture(int texture)179 public void glActiveTexture(int texture) { 180 mGLActiveTexture = texture; 181 } 182 makeColor4f(float red, float green, float blue, float alpha)183 public static int makeColor4f(float red, float green, float blue, 184 float alpha) { 185 return (Math.round(alpha * 255) << 24) | 186 (Math.round(red * 255) << 16) | 187 (Math.round(green * 255) << 8) | 188 Math.round(blue * 255); 189 } 190 makeColor4x(int red, int green, int blue, int alpha)191 public static int makeColor4x(int red, int green, int blue, int alpha) { 192 final float X = 65536f; 193 return makeColor4f(red / X, green / X, blue / X, alpha / X); 194 } 195 } 196