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/src/gpu/GrBatchedTextContext.cpp b/src/gpu/GrBatchedTextContext.cpp
new file mode 100644
index 0000000..3b3a4ba
--- /dev/null
+++ b/src/gpu/GrBatchedTextContext.cpp
@@ -0,0 +1,97 @@
+
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+
+#include "GrBatchedTextContext.h"
+#include "GrContext.h"
+#include "GrDrawTarget.h"
+#include "GrIndexBuffer.h"
+#include "GrTextContext.h"
+
+
+GrBatchedTextContext::GrBatchedTextContext() {
+}
+
+GrBatchedTextContext::~GrBatchedTextContext() {
+}
+
+void GrBatchedTextContext::init(GrContext* context,
+                                const GrPaint& grPaint,
+                                const GrMatrix* extMatrix) {
+    this->INHERITED::init(context, grPaint, extMatrix);
+    fGrPaint = grPaint;
+    fDrawTarget = fContext->getTextTarget(fGrPaint);
+
+    fMaxVertices = 0;
+    fCurrTexture = NULL;
+    fCurrVertex = 0;
+}
+
+void GrBatchedTextContext::finish() {
+    fDrawTarget = NULL;
+
+    this->INHERITED::finish();
+}
+
+void GrBatchedTextContext::reset() {
+    GrAssert(this->isValid());
+    fDrawTarget->resetVertexSource();
+    fMaxVertices = 0;
+    fCurrVertex = 0;
+    fCurrTexture->unref();
+    fCurrTexture = NULL;
+}
+
+void GrBatchedTextContext::prepareForGlyph(GrTexture* texture) {
+    GrAssert(this->isValid());
+    GrAssert(texture);
+    if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
+        this->flush();
+        fCurrTexture = texture;
+        fCurrTexture->ref();
+    }
+}
+
+void GrBatchedTextContext::setupVertexBuff(void** vertexBuff,
+                                           GrVertexLayout vertexLayout) {
+    GrAssert(this->isValid());
+    if (NULL == *vertexBuff) {
+        // If we need to reserve vertices allow the draw target to suggest
+        // a number of verts to reserve and whether to perform a flush.
+        fMaxVertices = kMinRequestedVerts;
+        bool flush = fDrawTarget->geometryHints(vertexLayout,
+                                                &fMaxVertices,
+                                                NULL);
+        if (flush) {
+            this->flush();
+            fContext->flushText();
+            fDrawTarget = fContext->getTextTarget(fGrPaint);
+            fMaxVertices = kDefaultRequestedVerts;
+            // ignore return, no point in flushing again.
+            fDrawTarget->geometryHints(vertexLayout,
+                                       &fMaxVertices,
+                                       NULL);
+        }
+
+        int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
+        if (fMaxVertices < kMinRequestedVerts) {
+            fMaxVertices = kDefaultRequestedVerts;
+        } else if (fMaxVertices > maxQuadVertices) {
+            // don't exceed the limit of the index buffer
+            fMaxVertices = maxQuadVertices;
+        }
+        bool success = fDrawTarget->reserveVertexAndIndexSpace(
+                                                   vertexLayout,
+                                                   fMaxVertices,
+                                                   0,
+                                                   vertexBuff,
+                                                   NULL);
+        GrAlwaysAssert(success);
+    }
+}