Add glOrtho equivalent to the OpenGL ES 2.0 renderer.

Change-Id: I063dad3d81dab7833acb1e7a9c7121f8efd2a044
diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk
index 44b6846b5..9544fe69 100644
--- a/libs/hwui/Android.mk
+++ b/libs/hwui/Android.mk
@@ -2,6 +2,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
+	UIMatrix.cpp \
 	UIOpenGLRenderer.cpp
 
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
diff --git a/libs/hwui/UIMatrix.cpp b/libs/hwui/UIMatrix.cpp
new file mode 100644
index 0000000..954a525
--- /dev/null
+++ b/libs/hwui/UIMatrix.cpp
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "UIMatrix"
+
+#include <math.h>
+#include <stdlib.h>
+
+#include <utils/Log.h>
+
+#include "UIMatrix.h"
+
+namespace android {
+
+void Matrix4::loadIdentity() {
+	mMat[0]  = 1;
+	mMat[1]  = 0;
+	mMat[2]  = 0;
+	mMat[3]  = 0;
+
+	mMat[4]  = 0;
+	mMat[5]  = 1;
+	mMat[6]  = 0;
+	mMat[7]  = 0;
+
+	mMat[8]  = 0;
+	mMat[9]  = 0;
+	mMat[10] = 1;
+	mMat[11] = 0;
+
+	mMat[12] = 0;
+	mMat[13] = 0;
+	mMat[14] = 0;
+	mMat[15] = 1;
+}
+
+void Matrix4::load(const float* v) {
+	memcpy(mMat, v, sizeof(mMat));
+}
+
+void Matrix4::load(const Matrix4& v) {
+	memcpy(mMat, v.mMat, sizeof(mMat));
+}
+
+void Matrix4::copyTo(float* v) const {
+	memcpy(v, mMat, sizeof(mMat));
+}
+
+void Matrix4::loadTranslate(float x, float y, float z) {
+	loadIdentity();
+	mMat[12] = x;
+	mMat[13] = y;
+	mMat[14] = z;
+}
+
+void Matrix4::loadScale(float sx, float sy, float sz) {
+	loadIdentity();
+	mMat[0]  = sx;
+	mMat[5]  = sy;
+	mMat[10] = sz;
+}
+
+void Matrix4::loadRotate(float angle, float x, float y, float z) {
+	mMat[3]  = 0;
+	mMat[7]  = 0;
+	mMat[11] = 0;
+	mMat[12] = 0;
+	mMat[13] = 0;
+	mMat[14] = 0;
+	mMat[15] = 1;
+
+	angle *= float(M_PI / 180.0f);
+	float c = cosf(angle);
+	float s = sinf(angle);
+
+	const float length = sqrtf(x * x + y * y + z * z);
+	const float nc = 1.0f - c;
+	const float xy = x * y;
+	const float yz = y * z;
+	const float zx = z * x;
+	const float xs = x * s;
+	const float ys = y * s;
+	const float zs = z * s;
+
+	mMat[0]  = x * x * nc +  c;
+	mMat[4]  =    xy * nc - zs;
+	mMat[8]  =    zx * nc + ys;
+	mMat[1]  =    xy * nc + zs;
+	mMat[5]  = y * y * nc +  c;
+	mMat[9]  =    yz * nc - xs;
+	mMat[2]  =    zx * nc - ys;
+	mMat[6]  =    yz * nc + xs;
+	mMat[10] = z * z * nc +  c;
+}
+
+void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) {
+    for (int i = 0 ; i < 4 ; i++) {
+        float x = 0;
+        float y = 0;
+        float z = 0;
+        float w = 0;
+
+        for (int j = 0 ; j < 4 ; j++) {
+            const float e = v.get(i,j);
+            x += u.get(j, 0) * e;
+            y += u.get(j, 1) * e;
+            z += u.get(j, 2) * e;
+            w += u.get(j, 3) * e;
+        }
+
+        set(i, 0, x);
+        set(i, 1, y);
+        set(i, 2, z);
+        set(i, 3, w);
+    }
+}
+
+void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
+    loadIdentity();
+    mMat[0]  = 2 / (right - left);
+    mMat[5]  = 2 / (top - bottom);
+    mMat[10] = -2 / (far - near);
+    mMat[12] = -(right + left) / (right - left);
+    mMat[13] = -(top + bottom) / (top - bottom);
+    mMat[14] = -(far + near) / (far - near);
+}
+
+void Matrix4::dump() const {
+	LOGD("%f %f %f %f", mMat[0], mMat[4], mMat[ 8], mMat[12]);
+	LOGD("%f %f %f %f", mMat[1], mMat[5], mMat[ 9], mMat[13]);
+	LOGD("%f %f %f %f", mMat[2], mMat[6], mMat[10], mMat[14]);
+	LOGD("%f %f %f %f", mMat[3], mMat[7], mMat[11], mMat[15]);
+}
+
+};
diff --git a/libs/hwui/UIMatrix.h b/libs/hwui/UIMatrix.h
new file mode 100644
index 0000000..55d7c07
--- /dev/null
+++ b/libs/hwui/UIMatrix.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_UI_MATRIX_H
+#define ANDROID_UI_MATRIX_H
+
+namespace android {
+
+///////////////////////////////////////////////////////////////////////////////
+// Classes
+///////////////////////////////////////////////////////////////////////////////
+
+class Matrix4 {
+public:
+	Matrix4() {
+		loadIdentity();
+	}
+
+	Matrix4(const float* v) {
+		load(v);
+	}
+
+	Matrix4(const Matrix4& v) {
+		load(v);
+	}
+
+	void loadIdentity();
+
+	void load(const float* v);
+	void load(const Matrix4& v);
+
+	void loadTranslate(float x, float y, float z);
+	void loadScale(float sx, float sy, float sz);
+	void loadRotate(float angle, float x, float y, float z);
+	void loadMultiply(const Matrix4& u, const Matrix4& v);
+
+	void loadOrtho(float left, float right, float bottom, float top, float near, float far);
+
+	void multiply(const Matrix4& v) {
+		Matrix4 u;
+		u.loadMultiply(*this, v);
+		load(u);
+	}
+
+	void translate(float x, float y, float z) {
+		Matrix4 u;
+		u.loadTranslate(x, y, z);
+		multiply(u);
+	}
+
+	void scale(float sx, float sy, float sz) {
+		Matrix4 u;
+		u.loadScale(sx, sy, sz);
+		multiply(u);
+	}
+
+	void rotate(float angle, float x, float y, float z) {
+		Matrix4 u;
+		u.loadRotate(angle, x, y, z);
+		multiply(u);
+	}
+
+	void copyTo(float* v) const;
+
+	void dump() const;
+
+//private:
+    inline float get(int i, int j) const {
+        return mMat[i * 4 + j];
+    }
+
+    inline void set(int i, int j, float v) {
+    	mMat[i * 4 + j] = v;
+    }
+
+	float mMat[16];
+}; // class Matrix4
+
+///////////////////////////////////////////////////////////////////////////////
+// Types
+///////////////////////////////////////////////////////////////////////////////
+
+typedef Matrix4 mat4;
+
+}; // namespace android
+
+#endif // ANDROID_UI_MATRIX_H
diff --git a/libs/hwui/UIOpenGLRenderer.cpp b/libs/hwui/UIOpenGLRenderer.cpp
index 334be15..b6113da 100644
--- a/libs/hwui/UIOpenGLRenderer.cpp
+++ b/libs/hwui/UIOpenGLRenderer.cpp
@@ -27,6 +27,7 @@
 #include <GLES2/gl2ext.h>
 
 #include "UIOpenGLRenderer.h"
+#include "UIMatrix.h"
 
 namespace android {
 
@@ -39,11 +40,18 @@
 }
 
 void UIOpenGLRenderer::setViewport(int width, int height) {
-    LOGD("Setting viewport");
+    glViewport(0, 0, width, height);
+
+    mat4 ortho;
+    ortho.loadOrtho(0, width, height, 0, 0, 1);
+    ortho.copyTo(mOrthoMatrix);
 }
 
 void UIOpenGLRenderer::prepare() {
-    LOGD("Prepare");
+    glDisable(GL_SCISSOR_TEST);
+    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+    glClear(GL_COLOR_BUFFER_BIT);
+    glEnable(GL_SCISSOR_TEST);
 }
 
 }; // namespace android
diff --git a/libs/hwui/UIOpenGLRenderer.h b/libs/hwui/UIOpenGLRenderer.h
index b0fb73f..3b95e6a 100644
--- a/libs/hwui/UIOpenGLRenderer.h
+++ b/libs/hwui/UIOpenGLRenderer.h
@@ -26,6 +26,10 @@
 
     void setViewport(int width, int height);
     void prepare();
+
+private:
+    float mOrthoMatrix[16];
+
 };
 
 }; // namespace android