• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 type,
44              GrGLsizei stride,
45              size_t offsetInBytes,
46              int divisor = 0);
47 
48     /**
49      * This function enables the first 'enabledCount' vertex arrays and disables the rest.
50      */
51     void enableVertexArrays(const GrGLGpu*, int enabledCount);
52 
invalidate()53     void invalidate() {
54         int count = fAttribArrayStates.count();
55         for (int i = 0; i < count; ++i) {
56             fAttribArrayStates[i].invalidate();
57         }
58         fEnabledCountIsValid = false;
59     }
60 
61     /**
62      * The number of attrib arrays that this object is configured to track.
63      */
count()64     int count() const { return fAttribArrayStates.count(); }
65 
66 private:
67     static constexpr int kInvalidDivisor = -1;
68 
69     /**
70      * Tracks the state of glVertexAttribArray for an attribute index.
71      */
72     struct AttribArrayState {
invalidateAttribArrayState73         void invalidate() {
74             fVertexBufferUniqueID.makeInvalid();
75             fDivisor = kInvalidDivisor;
76         }
77 
78         GrGpuResource::UniqueID   fVertexBufferUniqueID;
79         GrVertexAttribType        fType;
80         GrGLsizei                 fStride;
81         size_t                    fOffset;
82         int                       fDivisor;
83     };
84 
85     SkSTArray<16, AttribArrayState, true>   fAttribArrayStates;
86     int                                     fNumEnabledArrays;
87     bool                                    fEnabledCountIsValid;
88 };
89 
90 /**
91  * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
92  * and is used to track the state of the vertex array to avoid redundant GL calls.
93  */
94 class GrGLVertexArray {
95 public:
96     GrGLVertexArray(GrGLint id, int attribCount);
97 
98     /**
99      * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
100      * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
101      * returned.
102      */
103     GrGLAttribArrayState* bind(GrGLGpu*);
104 
105     /**
106      * This is a version of the above function that also binds an index buffer to the vertex
107      * array object.
108      */
109     GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
110 
arrayID()111     GrGLuint arrayID() const { return fID; }
112 
113     void invalidateCachedState();
114 
115 private:
116     GrGLuint                  fID;
117     GrGLAttribArrayState      fAttribArrays;
118     GrGpuResource::UniqueID   fIndexBufferUniqueID;
119 };
120 
121 #endif
122