Refactor how Gr handles vertex and index data. GrGpu and GrInOrderDrawBuffer both GrBufferAllocPool to manage reserved and set-to-array vertex and index data.

rietveld issue 4188049

git-svn-id: http://skia.googlecode.com/svn/trunk@786 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/include/GrMemory.h b/gpu/include/GrMemory.h
index 673d0ab..be8b1d7 100644
--- a/gpu/include/GrMemory.h
+++ b/gpu/include/GrMemory.h
@@ -22,7 +22,10 @@
 
 class GrAutoMalloc : GrNoncopyable {
 public:
-    GrAutoMalloc(size_t bytes) : fPtr(GrMalloc(bytes)) {}
+    GrAutoMalloc() : fPtr(NULL), fAllocatedBytes(0){
+    }
+
+    GrAutoMalloc(size_t bytes) : fPtr(GrMalloc(bytes)), fAllocatedBytes(bytes) {}
     ~GrAutoMalloc() { GrFree(fPtr); }
 
     /**
@@ -31,6 +34,8 @@
      */
     void* get() const { return fPtr; }
 
+    size_t size() const { return fAllocatedBytes; }
+
     /**
      *  transfer ownership of the memory to the caller. It must be freed with
      *  a call to GrFree()
@@ -38,19 +43,44 @@
     void* detach() {
         void* ptr = fPtr;
         fPtr = NULL;    // we no longer own the block
+        fAllocatedBytes = 0;
         return ptr;
     }
-    
+
+    /**
+     *  Reallocates to a new size. May or may not call malloc. The contents
+     *  are not preserved. If growOnly is true it will never reduce the
+     *  allocated size.
+     */
+    void* realloc(size_t newSize, bool growOnly = false) {
+        bool alloc;
+        if (growOnly) {
+            alloc = newSize > fAllocatedBytes;
+        } else {
+            alloc = newSize != fAllocatedBytes;
+        }
+        if (alloc) {
+            GrFree(fPtr);
+            fPtr = newSize ? GrMalloc(newSize) : NULL;
+            fAllocatedBytes = newSize;
+        }
+        GrAssert(fAllocatedBytes >= newSize);
+        GR_DEBUGCODE(memset(fPtr, 0xEF, fAllocatedBytes));
+        return fPtr;
+    }
+
     /**
      *  free the block now. get() will now return NULL
      */
     void free() {
         GrFree(fPtr);
         fPtr = NULL;
+        fAllocatedBytes = 0;
     }
 
 private:
     void* fPtr;
+    size_t fAllocatedBytes;
 };
 
 /**
@@ -86,10 +116,10 @@
      *  detached.
      */
     void* get() const { return fPtr; }
-    
+
     /**
-     *  Reallocates to a new size. May or may not call malloc. The contents 
-     *  are not preserved. If growOnly is true it will never reduce the 
+     *  Reallocates to a new size. May or may not call malloc. The contents
+     *  are not preserved. If growOnly is true it will never reduce the
      *  allocated size.
      */
     void* realloc(size_t newSize, bool growOnly = false) {
@@ -101,7 +131,7 @@
                 GrFree(fPtr);
                 fPtr = fStorage;
                 fAllocatedBytes = SIZE;
-            }            
+            }
         } else if ((newSize > fAllocatedBytes) ||
                    (!growOnly && newSize < (fAllocatedBytes >> 1))) {
             if (NULL != fPtr && fPtr != (void*)fStorage) {
@@ -115,7 +145,7 @@
         GR_DEBUGCODE(memset(fPtr, 0xEF, fAllocatedBytes));
         return fPtr;
     }
-   
+
     /**
      *  free the block now. get() will now return NULL
      */
@@ -126,7 +156,7 @@
         fAllocatedBytes = 0;
         fPtr = NULL;
     }
-    
+
 private:
     void*    fPtr;
     uint32_t fAllocatedBytes;