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         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              const GrBuffer* vertexBuffer,
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 
62     /**
63      * The number of attrib arrays that this object is configured to track.
64      */
count()65     int count() const { return fAttribArrayStates.count(); }
66 
67 private:
68     /**
69      * Tracks the state of glVertexAttribArray for an attribute index.
70      */
71     struct AttribArrayState {
invalidateAttribArrayState72         void invalidate() {
73             fEnableIsValid = false;
74             fVertexBufferUniqueID.makeInvalid();
75         }
76 
77         bool                            fEnableIsValid;
78         bool                            fEnabled;
79         GrGpuResource::UniqueID         fVertexBufferUniqueID;
80         GrVertexAttribType              fType;
81         GrGLsizei                       fStride;
82         GrGLvoid*                       fOffset;
83     };
84 
85     SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
86 };
87 
88 /**
89  * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
90  * and is used to track the state of the vertex array to avoid redundant GL calls.
91  */
92 class GrGLVertexArray {
93 public:
94     GrGLVertexArray(GrGLint id, int attribCount);
95 
96     /**
97      * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
98      * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
99      * returned.
100      */
101     GrGLAttribArrayState* bind(GrGLGpu*);
102 
103     /**
104      * This is a version of the above function that also binds an index buffer to the vertex
105      * array object.
106      */
107     GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
108 
arrayID()109     GrGLuint arrayID() const { return fID; }
110 
111     void invalidateCachedState();
112 
113 private:
114     GrGLuint                  fID;
115     GrGLAttribArrayState      fAttribArrays;
116     GrGpuResource::UniqueID   fIndexBufferUniqueID;
117 };
118 
119 #endif
120