add experimental state tracker for opengl



git-svn-id: http://skia.googlecode.com/svn/trunk@211 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gl/SkGLState.cpp b/src/gl/SkGLState.cpp
new file mode 100644
index 0000000..6a3cbc0
--- /dev/null
+++ b/src/gl/SkGLState.cpp
@@ -0,0 +1,155 @@
+#include "SkGLState.h"
+#include "SkColorPriv.h"
+
+// here is our global instance
+SkGLState SkGLState::gState;
+
+// this is an illegal pmcolor, since its alpha (0) is less than its red
+#define UNKNOWN_PMCOLOR (SK_R32_MASK << SK_R32_SHIFT)
+
+#define UNKNOWN_GLENUM  ((GLenum)-1)
+
+// MUST be in the same order as SkGLState::Caps enum
+static const GLenum gCapsTable[] = {
+    GL_DITHER,
+    GL_TEXTURE_2D,
+};
+
+// MUST be in the same order as SkGLState::ClientState enum
+static const GLenum gClientStateTable[] = {
+    GL_TEXTURE_COORD_ARRAY,
+    GL_COLOR_ARRAY,
+};
+
+static const GLenum gShadeModelTable[] = {
+    GL_FLAT,
+    GL_SMOOTH
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+SkGLState::SkGLState() :
+        fCapsPtr(gCapsTable),
+        fClientPtr(gClientStateTable),
+        fShadePtr(gShadeModelTable) {
+    this->init();
+}
+
+void SkGLState::init() {
+    fCapBits = 0;
+    fClientStateBits = 0;
+    fShadeModel = UNKNOWN_GLENUM;
+    fScissorSize.set(-1, -1);
+    fPMColor = UNKNOWN_PMCOLOR;
+    fSrcBlend = fDstBlend = UNKNOWN_GLENUM;
+    fPointSize = fLineWidth = -1;
+}
+
+void SkGLState::reset() {
+    this->init();
+
+    size_t i;
+    for (i = 0; i < SK_ARRAY_COUNT(gCapsTable); i++) {
+        glDisable(fCapsPtr[i]);
+    }
+    for (i = 0; i < SK_ARRAY_COUNT(gClientStateTable); i++) {
+        glDisableClientState(fClientPtr[i]);
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+void SkGLState::enable(Caps c) {
+    unsigned mask = 1 << c;
+    if ((fCapBits & mask) == 0) {
+        fCapBits |= mask;
+        glEnable(fCapsPtr[c]);
+    }
+}
+
+void SkGLState::disable(Caps c) {
+    unsigned mask = 1 << c;
+    if (fCapBits & mask) {
+        fCapBits &= ~mask;
+        glDisable(fCapsPtr[c]);
+    }
+}
+
+void SkGLState::enableClientState(ClientState c) {
+    unsigned mask = 1 << c;
+    if ((fClientStateBits & mask) == 0) {
+        fClientStateBits |= mask;
+        glEnableClientState(fClientPtr[c]);
+    }
+}
+
+void SkGLState::disableClientState(ClientState c) {
+    unsigned mask = 1 << c;
+    if (fClientStateBits & mask) {
+        fClientStateBits &= ~mask;
+        glDisableClientState(fClientPtr[c]);
+    }
+}
+
+void SkGLState::shadeModel(ShadeModel s) {
+    if (fShadeModel != s) {
+        fShadeModel = s;
+        glShadeModel(fShadePtr[s]);
+    }
+}
+
+void SkGLState::scissor(int x, int y, int w, int h) {
+    SkASSERT(w >= 0 && h >= 0);
+    if (!fScissorLoc.equals(x, y) || !fScissorSize.equals(w, h)) {
+        fScissorLoc.set(x, y);
+        fScissorSize.set(w, h);
+        glScissor(x, y, w, h);
+    }
+}
+
+void SkGLState::pointSize(float x) {
+    if (fPointSize != x) {
+        fPointSize = x;
+        glPointSize(x);
+    }
+}
+
+void SkGLState::lineWidth(float x) {
+    if (fLineWidth != x) {
+        fLineWidth = x;
+        glLineWidth(x);
+    }
+}
+
+void SkGLState::blendFunc(GLenum src, GLenum dst) {
+    if (fSrcBlend != src || fDstBlend != dst) {
+        fSrcBlend = src;
+        fDstBlend = dst;
+        glBlendFunc(src, dst);
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+#ifdef SK_GL_HAS_COLOR4UB
+    static inline void gl_pmcolor(U8CPU r, U8CPU g, U8CPU b, U8CPU a) {
+        glColor4ub(r, g, b, a);
+    }
+#else
+    static inline SkFixed byte2fixed(U8CPU value) {
+        return ((value << 8) | value) + (value >> 7);
+    }
+
+    static inline void gl_pmcolor(U8CPU r, U8CPU g, U8CPU b, U8CPU a) {
+        glColor4x(byte2fixed(r), byte2fixed(g), byte2fixed(b), byte2fixed(a));
+    }
+#endif
+
+void SkGLState::pmColor(SkPMColor c) {
+    if (fPMColor != c) {
+        fPMColor = c;
+        gl_pmcolor(SkGetPackedR32(c), SkGetPackedG32(c),
+                   SkGetPackedB32(c), SkGetPackedA32(c));
+    }
+}
+
diff --git a/src/gl/SkGLState.h b/src/gl/SkGLState.h
new file mode 100644
index 0000000..565498e
--- /dev/null
+++ b/src/gl/SkGLState.h
@@ -0,0 +1,74 @@
+
+#ifndef SkGLState_DEFINED
+#define SkGLState_DEFINED
+
+#include "SkGL.h"
+#include "SkSize.h"
+
+class SkGLState {
+public:
+    static SkGLState& GlobalState() { return gState; }
+
+    SkGLState();
+    
+    void reset();
+
+    // internally, these are bit_shifts, so they must be 0, 1, ...
+    enum Caps {
+        kDITHER,
+        kTEXTURE_2D,
+    };
+    void enable(Caps);
+    void disable(Caps);
+    
+    // internally, these are bit_shifts, so they must be 0, 1, ...
+    enum ClientState {
+        kTEXTURE_COORD_ARRAY,
+        kCOLOR_ARRAY,
+    };
+    void enableClientState(ClientState);
+    void disableClientState(ClientState);
+
+    // we use -1 for unknown, so the enum must be >= 0
+    enum ShadeModel {
+        kFLAT,
+        kSMOOTH,
+    };
+    void shadeModel(ShadeModel);
+
+    void scissor(int x, int y, int w, int h);
+    
+    void color(SkColor c) {
+        this->pmColor(SkPreMultiplyColor(c));
+    }
+    void alpha(U8CPU a) {
+        this->pmColor((a << 24) | (a << 16) | (a << 8) | a);
+    }
+    void pmColor(SkPMColor);
+
+    void blendFunc(GLenum src, GLenum dst);
+    
+    void pointSize(float);
+    void lineWidth(float);
+
+private:
+    void init();
+
+    unsigned    fCapBits;
+    unsigned    fClientStateBits;
+    int         fShadeModel;
+    SkIPoint    fScissorLoc;
+    SkISize     fScissorSize;
+    SkPMColor   fPMColor;
+    GLenum      fSrcBlend, fDstBlend;
+    float       fPointSize;
+    float       fLineWidth;
+    
+    const GLenum* fCapsPtr;
+    const GLenum* fClientPtr;
+    const GLenum* fShadePtr;
+    
+    static SkGLState gState;
+};
+
+#endif
diff --git a/xcode/sampleapp_sdl/SDLApp.xcodeproj/project.pbxproj b/xcode/sampleapp_sdl/SDLApp.xcodeproj/project.pbxproj
index 93308af..847ae69 100644
--- a/xcode/sampleapp_sdl/SDLApp.xcodeproj/project.pbxproj
+++ b/xcode/sampleapp_sdl/SDLApp.xcodeproj/project.pbxproj
@@ -18,7 +18,6 @@
 		0064EE2A0FC72BEE00D71FB0 /* SampleCircle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDF50FC72BEE00D71FB0 /* SampleCircle.cpp */; };
 		0064EE2B0FC72BEE00D71FB0 /* SampleCull.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDF70FC72BEE00D71FB0 /* SampleCull.cpp */; };
 		0064EE2C0FC72BEE00D71FB0 /* SampleDither.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDF80FC72BEE00D71FB0 /* SampleDither.cpp */; };
-		0064EE2D0FC72BEE00D71FB0 /* SampleDrawLooper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDF90FC72BEE00D71FB0 /* SampleDrawLooper.cpp */; };
 		0064EE2E0FC72BEE00D71FB0 /* SampleEffects.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDFA0FC72BEE00D71FB0 /* SampleEffects.cpp */; };
 		0064EE2F0FC72BEE00D71FB0 /* SampleEmboss.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDFB0FC72BEE00D71FB0 /* SampleEmboss.cpp */; };
 		0064EE300FC72BEE00D71FB0 /* SampleEncode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDFC0FC72BEE00D71FB0 /* SampleEncode.cpp */; };
@@ -72,6 +71,9 @@
 		00A728490FD43E7600D5051F /* SampleMovie.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE0C0FC72BEE00D71FB0 /* SampleMovie.cpp */; };
 		00A7284D0FD43E8900D5051F /* SkMovie.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A7284B0FD43E8900D5051F /* SkMovie.cpp */; };
 		00A728DD0FD6EDA700D5051F /* SkConcaveToTriangles.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A728DC0FD6EDA700D5051F /* SkConcaveToTriangles.cpp */; };
+		00AF77290FE15339007F9650 /* SkGLState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00AF77280FE15339007F9650 /* SkGLState.cpp */; };
+		00AF77540FE155BE007F9650 /* SampleVertices.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE220FC72BEE00D71FB0 /* SampleVertices.cpp */; };
+		00AF77810FE19B56007F9650 /* SampleDrawLooper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EDF90FC72BEE00D71FB0 /* SampleDrawLooper.cpp */; };
 		2762F6040FCCC832002BD8B4 /* SampleShapes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE190FC72BEE00D71FB0 /* SampleShapes.cpp */; };
 		2762F6420FCCCA6C002BD8B4 /* SkFlipPixelRef.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2762F6400FCCCA6C002BD8B4 /* SkFlipPixelRef.cpp */; };
 		2762F6430FCCCA6C002BD8B4 /* SkPageFlipper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2762F6410FCCCA6C002BD8B4 /* SkPageFlipper.cpp */; };
@@ -84,7 +86,6 @@
 		277836500FCF89F9006549E4 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2778364F0FCF89F9006549E4 /* OpenGL.framework */; };
 		2779F2610FD61678005D376E /* SampleXfermodes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE230FC72BEE00D71FB0 /* SampleXfermodes.cpp */; };
 		2779F27E0FD61829005D376E /* SampleText.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE1C0FC72BEE00D71FB0 /* SampleText.cpp */; };
-		27E1AAA70FD0C51B00098FC5 /* SampleVertices.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE220FC72BEE00D71FB0 /* SampleVertices.cpp */; };
 		27E1AAE00FD0C9B500098FC5 /* SampleShaders.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE180FC72BEE00D71FB0 /* SampleShaders.cpp */; };
 		27E1AB2B0FD0D06600098FC5 /* SamplePageFlip.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE0F0FC72BEE00D71FB0 /* SamplePageFlip.cpp */; };
 		27F9A4C90FDF4CE900C1CE9A /* SamplePatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0064EE100FC72BEE00D71FB0 /* SamplePatch.cpp */; };
@@ -232,6 +233,7 @@
 		00A7284B0FD43E8900D5051F /* SkMovie.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkMovie.cpp; path = ../../src/images/SkMovie.cpp; sourceTree = SOURCE_ROOT; };
 		00A7284C0FD43E8900D5051F /* SkMovie_gif.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkMovie_gif.cpp; path = ../../src/images/SkMovie_gif.cpp; sourceTree = SOURCE_ROOT; };
 		00A728DC0FD6EDA700D5051F /* SkConcaveToTriangles.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkConcaveToTriangles.cpp; path = ../../src/core/SkConcaveToTriangles.cpp; sourceTree = SOURCE_ROOT; };
+		00AF77280FE15339007F9650 /* SkGLState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkGLState.cpp; path = ../../src/gl/SkGLState.cpp; sourceTree = SOURCE_ROOT; };
 		089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
 		1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
 		2762F6400FCCCA6C002BD8B4 /* SkFlipPixelRef.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkFlipPixelRef.cpp; path = ../../src/images/SkFlipPixelRef.cpp; sourceTree = SOURCE_ROOT; };
@@ -421,6 +423,7 @@
 		277836430FCF890D006549E4 /* opengl */ = {
 			isa = PBXGroup;
 			children = (
+				00AF77280FE15339007F9650 /* SkGLState.cpp */,
 				00A728DC0FD6EDA700D5051F /* SkConcaveToTriangles.cpp */,
 				277836370FCF8908006549E4 /* SkGL.cpp */,
 				277836380FCF8908006549E4 /* SkGLCanvas.cpp */,
@@ -585,7 +588,6 @@
 				0064EE2A0FC72BEE00D71FB0 /* SampleCircle.cpp in Sources */,
 				0064EE2B0FC72BEE00D71FB0 /* SampleCull.cpp in Sources */,
 				0064EE2C0FC72BEE00D71FB0 /* SampleDither.cpp in Sources */,
-				0064EE2D0FC72BEE00D71FB0 /* SampleDrawLooper.cpp in Sources */,
 				0064EE2E0FC72BEE00D71FB0 /* SampleEffects.cpp in Sources */,
 				0064EE2F0FC72BEE00D71FB0 /* SampleEmboss.cpp in Sources */,
 				0064EE300FC72BEE00D71FB0 /* SampleEncode.cpp in Sources */,
@@ -641,7 +643,6 @@
 				277836400FCF8908006549E4 /* SkGLDevice_FBO.cpp in Sources */,
 				277836410FCF8908006549E4 /* SkGLTextCache.cpp in Sources */,
 				277836420FCF8908006549E4 /* SkTextureCache.cpp in Sources */,
-				27E1AAA70FD0C51B00098FC5 /* SampleVertices.cpp in Sources */,
 				27E1AAE00FD0C9B500098FC5 /* SampleShaders.cpp in Sources */,
 				27E1AB2B0FD0D06600098FC5 /* SamplePageFlip.cpp in Sources */,
 				00A728490FD43E7600D5051F /* SampleMovie.cpp in Sources */,
@@ -652,6 +653,9 @@
 				009611E90FD94A7E0053956C /* SampleAll.cpp in Sources */,
 				27F9A4C90FDF4CE900C1CE9A /* SamplePatch.cpp in Sources */,
 				27F9A4CC0FDF4D0300C1CE9A /* SamplePoints.cpp in Sources */,
+				00AF77290FE15339007F9650 /* SkGLState.cpp in Sources */,
+				00AF77540FE155BE007F9650 /* SampleVertices.cpp in Sources */,
+				00AF77810FE19B56007F9650 /* SampleDrawLooper.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};