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