Fix crash when forgot to call glVertexAttribPointer

Calling glDraw* will crash the emulatorif one forgot to call
glVertexAttribPointer first. This patch fixes it.

There are two situations: (1) user forgot to enable any vertex
attribute arrays, or (2) user enabled a vertex attribute array but
forgot to bind any data to it. This patch fix both situations. After the
patch, in (1) user will get an error message, in (2) user will get an
error message as well as GL error code GL_INVALID_OPERATION. These are
similar behaviors as running it in swift shader or on a real device.

(Running it on a real device with (1) will give nothing and with (2)
will crash the app.)

Change-Id: Id045c0c81a489a7c6ee4b6efc91f5ab148432f35
diff --git a/system/GLESv2_enc/GL2Encoder.cpp b/system/GLESv2_enc/GL2Encoder.cpp
index 25e09d9..a239445 100755
--- a/system/GLESv2_enc/GL2Encoder.cpp
+++ b/system/GLESv2_enc/GL2Encoder.cpp
@@ -462,6 +462,27 @@
 void GL2Encoder::s_glDrawArrays(void *self, GLenum mode, GLint first, GLsizei count)
 {
     GL2Encoder *ctx = (GL2Encoder *)self;
+
+    bool has_arrays = false;
+    int nLocations = ctx->m_state->nLocations();
+    for (int i = 0; i < nLocations; i++) {
+        const GLClientState::VertexAttribState *state = ctx->m_state->getState(i);
+        if (state->enabled) {
+            if (state->bufferObject || state->data)  {
+                has_arrays = true;
+            }
+            else {
+                ALOGE("glDrawArrays: a vertex attribute array is enabled with no data bound\n");
+                ctx->setError(GL_INVALID_OPERATION);
+                return;
+            }
+        }
+    }
+    if (!has_arrays) {
+        ALOGE("glDrawArrays: no data bound to the command - ignoring\n");
+        return;
+    }
+
     ctx->sendVertexAttributes(first, count);
     ctx->m_glDrawArrays_enc(ctx, mode, 0, count);
 }
@@ -483,8 +504,12 @@
         if (state->enabled) {
             if (state->bufferObject != 0) {
                 has_indirect_arrays = true;
-            } else {
+            } else if (state->data) {
                 has_immediate_arrays = true;
+            } else {
+                ALOGW("glDrawElements: a vertex attribute array is enabled with no data bound\n");
+                ctx->setError(GL_INVALID_OPERATION);
+                return;
             }
         }
     }