1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrGLVertexArray_DEFINED 9 #define GrGLVertexArray_DEFINED 10 11 #include "GrTypesPriv.h" 12 #include "gl/GrGLDefines.h" 13 #include "gl/GrGLTypes.h" 14 #include "SkTArray.h" 15 16 class GrGLVertexBuffer; 17 class GrGLIndexBuffer; 18 class GrGLGpu; 19 20 /** 21 * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray 22 * (below) but is separate because it is also used to track the state of vertex array object 0. 23 */ 24 class GrGLAttribArrayState { 25 public: 26 explicit GrGLAttribArrayState(int arrayCount = 0) { 27 this->resize(arrayCount); 28 } 29 resize(int newCount)30 void resize(int newCount) { 31 fAttribArrayStates.resize_back(newCount); 32 for (int i = 0; i < newCount; ++i) { 33 fAttribArrayStates[i].invalidate(); 34 } 35 } 36 37 /** 38 * This function enables and sets vertex attrib state for the specified attrib index. It is 39 * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex 40 * array object. 41 */ 42 void set(GrGLGpu*, 43 int attribIndex, 44 GrGLuint vertexBufferID, 45 GrVertexAttribType type, 46 GrGLsizei stride, 47 GrGLvoid* offset); 48 49 /** 50 * This function disables vertex attribs not present in the mask. It is assumed that the 51 * GrGLAttribArrayState is tracking the state of the currently bound vertex array object. 52 */ 53 void disableUnusedArrays(const GrGLGpu*, uint64_t usedAttribArrayMask); 54 invalidate()55 void invalidate() { 56 int count = fAttribArrayStates.count(); 57 for (int i = 0; i < count; ++i) { 58 fAttribArrayStates[i].invalidate(); 59 } 60 } 61 notifyVertexBufferDelete(GrGLuint id)62 void notifyVertexBufferDelete(GrGLuint id) { 63 int count = fAttribArrayStates.count(); 64 for (int i = 0; i < count; ++i) { 65 if (fAttribArrayStates[i].fAttribPointerIsValid && 66 id == fAttribArrayStates[i].fVertexBufferID) { 67 fAttribArrayStates[i].invalidate(); 68 } 69 } 70 } 71 72 /** 73 * The number of attrib arrays that this object is configured to track. 74 */ count()75 int count() const { return fAttribArrayStates.count(); } 76 77 private: 78 /** 79 * Tracks the state of glVertexAttribArray for an attribute index. 80 */ 81 struct AttribArrayState { invalidateAttribArrayState82 void invalidate() { 83 fEnableIsValid = false; 84 fAttribPointerIsValid = false; 85 } 86 87 bool fEnableIsValid; 88 bool fAttribPointerIsValid; 89 bool fEnabled; 90 GrGLuint fVertexBufferID; 91 GrVertexAttribType fType; 92 GrGLsizei fStride; 93 GrGLvoid* fOffset; 94 }; 95 96 SkSTArray<16, AttribArrayState, true> fAttribArrayStates; 97 }; 98 99 /** 100 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array 101 * and is used to track the state of the vertex array to avoid redundant GL calls. 102 */ 103 class GrGLVertexArray { 104 public: 105 GrGLVertexArray(GrGLint id, int attribCount); 106 107 /** 108 * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned. 109 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is 110 * returned. 111 */ 112 GrGLAttribArrayState* bind(GrGLGpu*); 113 114 /** 115 * This is a version of the above function that also binds an index buffer to the vertex 116 * array object. 117 */ 118 GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, GrGLuint indexBufferID); 119 120 void notifyIndexBufferDelete(GrGLuint bufferID); 121 notifyVertexBufferDelete(GrGLuint id)122 void notifyVertexBufferDelete(GrGLuint id) { 123 fAttribArrays.notifyVertexBufferDelete(id); 124 } 125 arrayID()126 GrGLuint arrayID() const { return fID; } 127 128 void invalidateCachedState(); 129 130 private: 131 GrGLuint fID; 132 GrGLAttribArrayState fAttribArrays; 133 GrGLuint fIndexBufferID; 134 bool fIndexBufferIDIsValid; 135 }; 136 137 #endif 138