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 "GrGpuResource.h"
12 #include "GrTypesPriv.h"
13 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLTypes.h"
15 #include "SkTArray.h"
16 
17 class GrBuffer;
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         this->invalidate();
33     }
34 
35     /**
36      * This function enables and sets vertex attrib state for the specified attrib index. It is
37      * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex
38      * array object.
39      */
40     void set(GrGLGpu*,
41              int attribIndex,
42              const GrBuffer* vertexBuffer,
43              GrVertexAttribType cpuType,
44              GrSLType gpuType,
45              GrGLsizei stride,
46              size_t offsetInBytes,
47              int divisor = 0);
48 
49     /**
50      * This function enables the first 'enabledCount' vertex arrays and disables the rest.
51      */
52     void enableVertexArrays(const GrGLGpu*, int enabledCount,
53                             GrPrimitiveRestart = GrPrimitiveRestart::kNo);
54 
invalidate()55     void invalidate() {
56         int count = fAttribArrayStates.count();
57         for (int i = 0; i < count; ++i) {
58             fAttribArrayStates[i].invalidate();
59         }
60         fEnableStateIsValid = false;
61     }
62 
63     /**
64      * The number of attrib arrays that this object is configured to track.
65      */
count()66     int count() const { return fAttribArrayStates.count(); }
67 
68 private:
69     static constexpr int kInvalidDivisor = -1;
70 
71     /**
72      * Tracks the state of glVertexAttribArray for an attribute index.
73      */
74     struct AttribArrayState {
invalidateAttribArrayState75         void invalidate() {
76             fVertexBufferUniqueID.makeInvalid();
77             fDivisor = kInvalidDivisor;
78         }
79 
80         GrGpuResource::UniqueID   fVertexBufferUniqueID;
81         GrVertexAttribType        fCPUType;
82         GrSLType                  fGPUType;
83         GrGLsizei                 fStride;
84         size_t                    fOffset;
85         int                       fDivisor;
86     };
87 
88     SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
89     int fNumEnabledArrays;
90     GrPrimitiveRestart fPrimitiveRestartEnabled;
91     bool fEnableStateIsValid = false;
92 };
93 
94 /**
95  * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
96  * and is used to track the state of the vertex array to avoid redundant GL calls.
97  */
98 class GrGLVertexArray {
99 public:
100     GrGLVertexArray(GrGLint id, int attribCount);
101 
102     /**
103      * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
104      * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
105      * returned.
106      */
107     GrGLAttribArrayState* bind(GrGLGpu*);
108 
109     /**
110      * This is a version of the above function that also binds an index buffer to the vertex
111      * array object.
112      */
113     GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
114 
arrayID()115     GrGLuint arrayID() const { return fID; }
116 
117     void invalidateCachedState();
118 
119 private:
120     GrGLuint                  fID;
121     GrGLAttribArrayState      fAttribArrays;
122     GrGpuResource::UniqueID   fIndexBufferUniqueID;
123 };
124 
125 #endif
126