Split GrTextContext into baseclass and subclass

This is a step towards enabling alternate text rendering code paths (GLyphy in particular)

Committed on behalf of baranowski@chromium.org

Review URL: http://codereview.appspot.com/5796071/



git-svn-id: http://skia.googlecode.com/svn/trunk@3412 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrTextContext.h b/include/gpu/GrTextContext.h
index 5983e35..588ae6e 100644
--- a/include/gpu/GrTextContext.h
+++ b/include/gpu/GrTextContext.h
@@ -12,57 +12,99 @@
 #define GrTextContext_DEFINED
 
 #include "GrGlyph.h"
-#include "GrPaint.h"
 #include "GrMatrix.h"
+#include "GrRefCnt.h"
 
-struct GrGpuTextVertex;
 class GrContext;
-class GrTextStrike;
 class GrFontScaler;
-class GrDrawTarget;
+class GrPaint;
 
-class GrTextContext {
-public:
-    GrTextContext(GrContext*,
-                  const GrPaint& paint,
-                  const GrMatrix* extMatrix = NULL);
-    ~GrTextContext();
+class SkGpuDevice;
+class SkPaint;
 
-    void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
-                         GrFontScaler*);
-
-    void flush();   // optional; automatically called by destructor
-
-private:
-    GrPaint         fPaint;
-    GrVertexLayout  fVertexLayout;
+class GrTextContext: public GrRefCnt {
+protected:
     GrContext*      fContext;
-    GrDrawTarget*   fDrawTarget;
 
-    GrMatrix        fExtMatrix;
-    GrFontScaler*   fScaler;
-    GrTextStrike*   fStrike;
+public:
+    /**
+     * To use a text context it must be wrapped in an AutoFinish. AutoFinish's
+     * destructor ensures all drawing is flushed to the GrContext.
+     */
+    class AutoFinish {
+    public:
+        AutoFinish(GrTextContext* textContext, GrContext* context,
+                   const GrPaint&, const GrMatrix* extMatrix);
+        ~AutoFinish();
+        GrTextContext* getTextContext() const;
 
-    inline void flushGlyphs();
-    void setupDrawTarget();
-
-    enum {
-        kMinRequestedGlyphs      = 1,
-        kDefaultRequestedGlyphs  = 64,
-        kMinRequestedVerts       = kMinRequestedGlyphs * 4,
-        kDefaultRequestedVerts   = kDefaultRequestedGlyphs * 4,
+    private:
+        GrTextContext* fTextContext;
     };
 
-    GrGpuTextVertex* fVertices;
+    virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
+                                 GrFontScaler*) = 0;
 
-    int32_t     fMaxVertices;
-    GrTexture*  fCurrTexture;
-    int         fCurrVertex;
+    virtual ~GrTextContext() {}
 
-    GrIRect     fClipRect;
-    GrMatrix    fOrigViewMatrix;    // restore previous viewmatrix
+protected:
+    GrTextContext() {
+        fContext = NULL;
+    }
+
+    bool isValid() const {
+        return (NULL != fContext);
+    }
+
+    /**
+     * Initialize the object.
+     *
+     * Before call to this method, the instance is considered to be in
+     * invalid state. I.e. call to any method other than isValid will result in
+     * undefined behaviour.
+     *
+     * @see finish
+     */
+    virtual void init(GrContext* context, const GrPaint&,
+                      const GrMatrix* extMatrix) {
+        fContext = context;
+    }
+
+    /**
+     * Reset the object to invalid state.
+     *
+     * After call to this method, the instance is considered to be in
+     * invalid state.
+     *
+     * It might be brought back to a valid state by calling init.
+     *
+     * @see init
+     */
+    virtual void finish() {
+        fContext = NULL;
+    }
+
+private:
+    typedef GrRefCnt INHERITED;
 };
 
+inline GrTextContext::AutoFinish::AutoFinish(GrTextContext* textContext,
+                                             GrContext* context,
+                                             const GrPaint& grPaint,
+                                             const GrMatrix* extMatrix) {
+    GrAssert(NULL != textContext);
+    fTextContext = textContext;
+    fTextContext->ref();
+    fTextContext->init(context, grPaint, extMatrix);
+}
+
+inline GrTextContext::AutoFinish::~AutoFinish() {
+    fTextContext->finish();
+    fTextContext->unref();
+}
+
+inline GrTextContext* GrTextContext::AutoFinish::getTextContext() const {
+    return fTextContext;
+}
+
 #endif
-
-
diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h
index 46c15d6..24e6e5a 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -147,6 +147,8 @@
     bool                fNeedClear;
     bool                fNeedPrepareRenderTarget;
 
+    GrTextContext*      fTextContext;
+
     // called from rt and tex cons
     void initFromRenderTarget(GrContext*, GrRenderTarget*);
 
@@ -167,6 +169,11 @@
     void internalDrawBitmap(const SkDraw&, const SkBitmap&,
                             const SkIRect&, const SkMatrix&, GrPaint* grPaint);
 
+    /**
+     * Returns non-initialized instance.
+     */
+    GrTextContext* getTextContext();
+
     typedef SkDevice INHERITED;
 };