Glop support for indexed quads

bug:19014311
Change-Id: If35a873421b41cc4508b0d8ac1b4d900c9bb3717
diff --git a/libs/hwui/renderstate/MeshState.cpp b/libs/hwui/renderstate/MeshState.cpp
index ce6030d..5efac0e 100644
--- a/libs/hwui/renderstate/MeshState.cpp
+++ b/libs/hwui/renderstate/MeshState.cpp
@@ -31,13 +31,29 @@
         , mCurrentTexCoordsStride(0)
         , mTexCoordsArrayEnabled(false)
         , mQuadListIndices(0) {
-
     glGenBuffers(1, &mUnitQuadBuffer);
     glBindBuffer(GL_ARRAY_BUFFER, mUnitQuadBuffer);
     glBufferData(GL_ARRAY_BUFFER, sizeof(kUnitQuadVertices), kUnitQuadVertices, GL_STATIC_DRAW);
 
     mCurrentBuffer = mUnitQuadBuffer;
 
+    std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[kMaxNumberOfQuads * 6]);
+    for (uint32_t i = 0; i < kMaxNumberOfQuads; i++) {
+        uint16_t quad = i * 4;
+        int index = i * 6;
+        regionIndices[index    ] = quad;       // top-left
+        regionIndices[index + 1] = quad + 1;   // top-right
+        regionIndices[index + 2] = quad + 2;   // bottom-left
+        regionIndices[index + 3] = quad + 2;   // bottom-left
+        regionIndices[index + 4] = quad + 1;   // top-right
+        regionIndices[index + 5] = quad + 3;   // bottom-right
+    }
+    glGenBuffers(1, &mQuadListIndices);
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mQuadListIndices);
+    glBufferData(GL_ELEMENT_ARRAY_BUFFER, kMaxNumberOfQuads * 6 * sizeof(uint16_t),
+            regionIndices.get(), GL_STATIC_DRAW);
+    mCurrentIndicesBuffer = mQuadListIndices;
+
     // position attribute always enabled
     glEnableVertexAttribArray(Program::kBindingPosition);
 }
@@ -138,26 +154,6 @@
 }
 
 bool MeshState::bindQuadIndicesBuffer() {
-    if (!mQuadListIndices) {
-        std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[kMaxNumberOfQuads * 6]);
-        for (uint32_t i = 0; i < kMaxNumberOfQuads; i++) {
-            uint16_t quad = i * 4;
-            int index = i * 6;
-            regionIndices[index    ] = quad;       // top-left
-            regionIndices[index + 1] = quad + 1;   // top-right
-            regionIndices[index + 2] = quad + 2;   // bottom-left
-            regionIndices[index + 3] = quad + 2;   // bottom-left
-            regionIndices[index + 4] = quad + 1;   // top-right
-            regionIndices[index + 5] = quad + 3;   // bottom-right
-        }
-
-        glGenBuffers(1, &mQuadListIndices);
-        bool force = bindIndicesBufferInternal(mQuadListIndices);
-        glBufferData(GL_ELEMENT_ARRAY_BUFFER, kMaxNumberOfQuads * 6 * sizeof(uint16_t),
-                regionIndices.get(), GL_STATIC_DRAW);
-        return force;
-    }
-
     return bindIndicesBufferInternal(mQuadListIndices);
 }
 
diff --git a/libs/hwui/renderstate/MeshState.h b/libs/hwui/renderstate/MeshState.h
index afc6267..3c92ad8 100644
--- a/libs/hwui/renderstate/MeshState.h
+++ b/libs/hwui/renderstate/MeshState.h
@@ -114,6 +114,7 @@
     // Getters - for use in Glop building
     ///////////////////////////////////////////////////////////////////////////////
     GLuint getUnitQuadVBO() { return mUnitQuadBuffer; }
+    GLuint getQuadListIBO() { return mQuadListIndices; }
 private:
     MeshState();
     bool bindMeshBufferInternal(const GLuint buffer);
diff --git a/libs/hwui/renderstate/RenderState.cpp b/libs/hwui/renderstate/RenderState.cpp
index ba49833..6394dc1 100644
--- a/libs/hwui/renderstate/RenderState.cpp
+++ b/libs/hwui/renderstate/RenderState.cpp
@@ -204,12 +204,7 @@
  *
  * Textures + coordinates
  * SkiaShader
- * ColorFilter
- *
-    // TODO: texture coord
-    // TODO: texture support
-    // TODO: skiashader support
-    // TODO: color filter support
+ * RoundRect clipping
  */
 
 void RenderState::render(const Glop& glop) {
@@ -251,18 +246,18 @@
     // indices
     meshState().bindIndicesBufferInternal(mesh.indexBufferObject);
 
-    if (glop.mesh.vertexFlags & kTextureCoord_Attrib) {
+    if (mesh.vertexFlags & kTextureCoord_Attrib) {
         // TODO: support textures
         LOG_ALWAYS_FATAL("textures not yet supported");
     } else {
         meshState().disableTexCoordsVertexArray();
     }
-    if (glop.mesh.vertexFlags & kColor_Attrib) {
+    if (mesh.vertexFlags & kColor_Attrib) {
         LOG_ALWAYS_FATAL("color vertex attribute not yet supported");
         // TODO: enable color, disable when done
     }
     int alphaSlot = -1;
-    if (glop.mesh.vertexFlags & kAlpha_Attrib) {
+    if (mesh.vertexFlags & kAlpha_Attrib) {
         const void* alphaCoords = ((const GLbyte*) glop.mesh.vertices) + kVertexAlphaOffset;
         alphaSlot = shader.program->getAttrib("vtxAlpha");
         glEnableVertexAttribArray(alphaSlot);
@@ -275,15 +270,31 @@
     blend().setFactors(glop.blend.src, glop.blend.dst);
 
     // ------------------------------------
-    // ---------- GL state setup ----------
+    // ---------- Actual drawing ----------
     // ------------------------------------
-    if (mesh.indexBufferObject || mesh.indices) {
-        glDrawElements(glop.mesh.primitiveMode, glop.mesh.vertexCount,
-                GL_UNSIGNED_SHORT, mesh.indices);
+    if (mesh.indexBufferObject == meshState().getQuadListIBO()) {
+        // Since the indexed quad list is of limited length, we loop over
+        // the glDrawXXX method while updating the vertex pointer
+        GLsizei elementsCount = mesh.vertexCount;
+        const GLbyte* vertices = static_cast<const GLbyte*>(mesh.vertices);
+        while (elementsCount > 0) {
+            GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
+
+            // TODO: this double binds on first pass
+            meshState().bindPositionVertexPointer(true, vertices, mesh.stride);
+            glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
+            elementsCount -= drawCount;
+            vertices += (drawCount / 6) * 4 * mesh.stride;
+        }
+    } else if (mesh.indexBufferObject || mesh.indices) {
+        glDrawElements(mesh.primitiveMode, mesh.vertexCount, GL_UNSIGNED_SHORT, mesh.indices);
     } else {
-        glDrawArrays(glop.mesh.primitiveMode, 0, glop.mesh.vertexCount);
+        glDrawArrays(mesh.primitiveMode, 0, mesh.vertexCount);
     }
 
+    // -----------------------------------
+    // ---------- Mesh teardown ----------
+    // -----------------------------------
     if (glop.mesh.vertexFlags & kAlpha_Attrib) {
         glDisableVertexAttribArray(alphaSlot);
     }
diff --git a/libs/hwui/renderstate/Stencil.h b/libs/hwui/renderstate/Stencil.h
index a88beae..e4f0f3f 100644
--- a/libs/hwui/renderstate/Stencil.h
+++ b/libs/hwui/renderstate/Stencil.h
@@ -98,6 +98,10 @@
         return mState == kTest;
     }
 
+    bool isWriteEnabled() {
+        return mState == kWrite;
+    }
+
     void dump();
 
 private: