xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gl/GrGLVertexArray.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/gpu/ganesh/gl/GrGLTypes.h"
12 #include "include/private/base/SkTArray.h"
13 #include "include/private/gpu/ganesh/GrTypesPriv.h"
14 #include "src/gpu/ganesh/GrGpuResource.h"
15 
16 #include <cstddef>
17 
18 class GrBuffer;
19 class GrGLGpu;
20 enum class SkSLType : char;
21 
22 /**
23  * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray
24  * (below) but is separate because it is also used to track the state of vertex array object 0.
25  */
26 class GrGLAttribArrayState {
27 public:
28     explicit GrGLAttribArrayState(int arrayCount = 0) {
29         this->resize(arrayCount);
30     }
31 
resize(int newCount)32     void resize(int newCount) {
33         fAttribArrayStates.resize_back(newCount);
34         this->invalidate();
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 cpuType,
46              SkSLType gpuType,
47              GrGLsizei stride,
48              size_t offsetInBytes,
49              int divisor = 0);
50 
51     /**
52      * This function enables the first 'enabledCount' vertex arrays and disables the rest.
53      */
54     void enableVertexArrays(const GrGLGpu*, int enabledCount,
55                             GrPrimitiveRestart = GrPrimitiveRestart::kNo);
56 
invalidate()57     void invalidate() {
58         int count = fAttribArrayStates.size();
59         for (int i = 0; i < count; ++i) {
60             fAttribArrayStates[i].invalidate();
61         }
62         fEnableStateIsValid = false;
63     }
64 
65     /**
66      * The number of attrib arrays that this object is configured to track.
67      */
count()68     int count() const { return fAttribArrayStates.size(); }
69 
70 private:
71     inline static constexpr int kInvalidDivisor = -1;
72 
73     /**
74      * Tracks the state of glVertexAttribArray for an attribute index.
75      */
76     struct AttribArrayState {
invalidateAttribArrayState77         void invalidate() {
78             fVertexBufferUniqueID.makeInvalid();
79             fDivisor = kInvalidDivisor;
80             fUsingCpuBuffer = false;
81         }
82 
83         GrGpuResource::UniqueID   fVertexBufferUniqueID;
84         bool                      fUsingCpuBuffer;
85         GrVertexAttribType        fCPUType;
86         SkSLType                  fGPUType;
87         GrGLsizei                 fStride;
88         const GrGLvoid*           fOffset;
89         int                       fDivisor;
90     };
91 
92     skia_private::STArray<16, AttribArrayState, true> fAttribArrayStates;
93     int fNumEnabledArrays;
94     GrPrimitiveRestart fPrimitiveRestartEnabled;
95     bool fEnableStateIsValid = false;
96 };
97 
98 /**
99  * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
100  * and is used to track the state of the vertex array to avoid redundant GL calls.
101  */
102 class GrGLVertexArray {
103 public:
104     GrGLVertexArray(GrGLint id, int attribCount);
105 
106     /**
107      * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
108      * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
109      * returned.
110      */
111     GrGLAttribArrayState* bind(GrGLGpu*);
112 
113     /**
114      * This is a version of the above function that also binds an index buffer to the vertex
115      * array object.
116      */
117     GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
118 
arrayID()119     GrGLuint arrayID() const { return fID; }
120 
121     void invalidateCachedState();
122 
123 private:
124     GrGLuint                  fID;
125     GrGLAttribArrayState      fAttribArrays;
126     GrGpuResource::UniqueID   fIndexBufferUniqueID;
127 };
128 
129 #endif
130