Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef GrGLVertexArray_DEFINED |
michael@0 | 9 | #define GrGLVertexArray_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "GrResource.h" |
michael@0 | 12 | #include "GrTypesPriv.h" |
michael@0 | 13 | #include "gl/GrGLDefines.h" |
michael@0 | 14 | #include "gl/GrGLFunctions.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "SkTArray.h" |
michael@0 | 17 | |
michael@0 | 18 | class GrGLVertexBuffer; |
michael@0 | 19 | class GrGLIndexBuffer; |
michael@0 | 20 | class GrGpuGL; |
michael@0 | 21 | |
michael@0 | 22 | struct GrGLAttribLayout { |
michael@0 | 23 | GrGLint fCount; |
michael@0 | 24 | GrGLenum fType; |
michael@0 | 25 | GrGLboolean fNormalized; |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | static inline const GrGLAttribLayout& GrGLAttribTypeToLayout(GrVertexAttribType type) { |
michael@0 | 29 | SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount); |
michael@0 | 30 | static const GrGLAttribLayout kLayouts[kGrVertexAttribTypeCount] = { |
michael@0 | 31 | {1, GR_GL_FLOAT, false}, // kFloat_GrVertexAttribType |
michael@0 | 32 | {2, GR_GL_FLOAT, false}, // kVec2f_GrVertexAttribType |
michael@0 | 33 | {3, GR_GL_FLOAT, false}, // kVec3f_GrVertexAttribType |
michael@0 | 34 | {4, GR_GL_FLOAT, false}, // kVec4f_GrVertexAttribType |
michael@0 | 35 | {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType |
michael@0 | 36 | }; |
michael@0 | 37 | GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType); |
michael@0 | 38 | GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType); |
michael@0 | 39 | GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType); |
michael@0 | 40 | GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType); |
michael@0 | 41 | GR_STATIC_ASSERT(4 == kVec4ub_GrVertexAttribType); |
michael@0 | 42 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayouts) == kGrVertexAttribTypeCount); |
michael@0 | 43 | return kLayouts[type]; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray |
michael@0 | 48 | * (below) but is separate because it is also used to track the state of vertex array object 0. |
michael@0 | 49 | */ |
michael@0 | 50 | class GrGLAttribArrayState { |
michael@0 | 51 | public: |
michael@0 | 52 | explicit GrGLAttribArrayState(int arrayCount = 0) { |
michael@0 | 53 | this->resize(arrayCount); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | void resize(int newCount) { |
michael@0 | 57 | fAttribArrayStates.resize_back(newCount); |
michael@0 | 58 | for (int i = 0; i < newCount; ++i) { |
michael@0 | 59 | fAttribArrayStates[i].invalidate(); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * This function enables and sets vertex attrib state for the specified attrib index. It is |
michael@0 | 65 | * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex |
michael@0 | 66 | * array object. |
michael@0 | 67 | */ |
michael@0 | 68 | void set(const GrGpuGL*, |
michael@0 | 69 | int index, |
michael@0 | 70 | GrGLVertexBuffer*, |
michael@0 | 71 | GrGLint size, |
michael@0 | 72 | GrGLenum type, |
michael@0 | 73 | GrGLboolean normalized, |
michael@0 | 74 | GrGLsizei stride, |
michael@0 | 75 | GrGLvoid* offset); |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * This function disables vertex attribs not present in the mask. It is assumed that the |
michael@0 | 79 | * GrGLAttribArrayState is tracking the state of the currently bound vertex array object. |
michael@0 | 80 | */ |
michael@0 | 81 | void disableUnusedArrays(const GrGpuGL*, uint64_t usedAttribArrayMask); |
michael@0 | 82 | |
michael@0 | 83 | void invalidate() { |
michael@0 | 84 | int count = fAttribArrayStates.count(); |
michael@0 | 85 | for (int i = 0; i < count; ++i) { |
michael@0 | 86 | fAttribArrayStates[i].invalidate(); |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | void notifyVertexBufferDelete(GrGLuint id) { |
michael@0 | 91 | int count = fAttribArrayStates.count(); |
michael@0 | 92 | for (int i = 0; i < count; ++i) { |
michael@0 | 93 | if (fAttribArrayStates[i].fAttribPointerIsValid && |
michael@0 | 94 | id == fAttribArrayStates[i].fVertexBufferID) { |
michael@0 | 95 | fAttribArrayStates[i].invalidate(); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | /** |
michael@0 | 101 | * The number of attrib arrays that this object is configured to track. |
michael@0 | 102 | */ |
michael@0 | 103 | int count() const { return fAttribArrayStates.count(); } |
michael@0 | 104 | |
michael@0 | 105 | private: |
michael@0 | 106 | /** |
michael@0 | 107 | * Tracks the state of glVertexAttribArray for an attribute index. |
michael@0 | 108 | */ |
michael@0 | 109 | struct AttribArrayState { |
michael@0 | 110 | void invalidate() { |
michael@0 | 111 | fEnableIsValid = false; |
michael@0 | 112 | fAttribPointerIsValid = false; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | bool fEnableIsValid; |
michael@0 | 116 | bool fAttribPointerIsValid; |
michael@0 | 117 | bool fEnabled; |
michael@0 | 118 | GrGLuint fVertexBufferID; |
michael@0 | 119 | GrGLint fSize; |
michael@0 | 120 | GrGLenum fType; |
michael@0 | 121 | GrGLboolean fNormalized; |
michael@0 | 122 | GrGLsizei fStride; |
michael@0 | 123 | GrGLvoid* fOffset; |
michael@0 | 124 | }; |
michael@0 | 125 | |
michael@0 | 126 | SkSTArray<16, AttribArrayState, true> fAttribArrayStates; |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array |
michael@0 | 131 | * and is used to track the state of the vertex array to avoid redundant GL calls. |
michael@0 | 132 | */ |
michael@0 | 133 | class GrGLVertexArray : public GrResource { |
michael@0 | 134 | public: |
michael@0 | 135 | GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount); |
michael@0 | 136 | |
michael@0 | 137 | /** |
michael@0 | 138 | * Binds this vertex array. If the ID has been deleted or abandoned then NULL is returned. |
michael@0 | 139 | * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is |
michael@0 | 140 | * returned. |
michael@0 | 141 | */ |
michael@0 | 142 | GrGLAttribArrayState* bind(); |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * This is a version of the above function that also binds an index buffer to the vertex |
michael@0 | 146 | * array object. |
michael@0 | 147 | */ |
michael@0 | 148 | GrGLAttribArrayState* bindWithIndexBuffer(const GrGLIndexBuffer* indexBuffer); |
michael@0 | 149 | |
michael@0 | 150 | void notifyIndexBufferDelete(GrGLuint bufferID); |
michael@0 | 151 | |
michael@0 | 152 | void notifyVertexBufferDelete(GrGLuint id) { |
michael@0 | 153 | fAttribArrays.notifyVertexBufferDelete(id); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | GrGLuint arrayID() const { return fID; } |
michael@0 | 157 | |
michael@0 | 158 | void invalidateCachedState(); |
michael@0 | 159 | |
michael@0 | 160 | virtual size_t sizeInBytes() const SK_OVERRIDE { return 0; } |
michael@0 | 161 | |
michael@0 | 162 | protected: |
michael@0 | 163 | virtual void onAbandon() SK_OVERRIDE; |
michael@0 | 164 | |
michael@0 | 165 | virtual void onRelease() SK_OVERRIDE; |
michael@0 | 166 | |
michael@0 | 167 | private: |
michael@0 | 168 | GrGLuint fID; |
michael@0 | 169 | GrGLAttribArrayState fAttribArrays; |
michael@0 | 170 | GrGLuint fIndexBufferID; |
michael@0 | 171 | bool fIndexBufferIDIsValid; |
michael@0 | 172 | |
michael@0 | 173 | typedef GrResource INHERITED; |
michael@0 | 174 | }; |
michael@0 | 175 | |
michael@0 | 176 | #endif |