blob: 5693f4a5e0c7b3f07f3b6054963d8a2fd6a6c2af [file] [log] [blame]
bsalomon@google.com6918d482013-03-07 19:09:11 +00001/*
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 "GrResource.h"
12#include "gl/GrGLFunctions.h"
13
14#include "SkTArray.h"
15
16class GrGLVertexBuffer;
17class GrGLIndexBuffer;
18class GrGpuGL;
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 */
24class GrGLAttribArrayState {
25public:
26 explicit GrGLAttribArrayState(int arrayCount = 0) { this->resize(arrayCount); }
27
28 void resize(int newCount) {
29 fAttribArrayStates.resize_back(newCount);
30 for (int i = 0; i < newCount; ++i) {
31 fAttribArrayStates[i].invalidate();
32 }
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(const GrGpuGL*,
41 int index,
42 GrGLVertexBuffer*,
43 GrGLint size,
44 GrGLenum type,
45 GrGLboolean normalized,
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 disableUnusedAttribArrays(const GrGpuGL*, uint64_t usedAttribArrayMask);
54
55 void invalidate() {
56 int count = fAttribArrayStates.count();
57 for (int i = 0; i < count; ++i) {
58 fAttribArrayStates[i].invalidate();
59 }
60 }
61
62 void notifyVertexBufferDelete(GrGLuint id) {
63 int count = fAttribArrayStates.count();
64 for (int i = 0; i < count; ++i) {
robertphillips@google.com152336f2013-03-13 21:47:56 +000065 if (fAttribArrayStates[i].fAttribPointerIsValid &&
66 id == fAttribArrayStates[i].fVertexBufferID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000067 fAttribArrayStates[i].invalidate();
68 }
69 }
70 }
71
72 /**
73 * The number of attrib arrays that this object is configured to track.
74 */
75 int count() const { return fAttribArrayStates.count(); }
76
77private:
78 /**
79 * Tracks the state of glVertexAttribArray for an attribute index.
80 */
81 struct AttribArrayState {
82 void invalidate() {
83 fEnableIsValid = false;
84 fAttribPointerIsValid = false;
85 }
86
87 bool fEnableIsValid;
88 bool fAttribPointerIsValid;
89 bool fEnabled;
90 GrGLuint fVertexBufferID;
91 GrGLint fSize;
92 GrGLenum fType;
93 GrGLboolean fNormalized;
94 GrGLsizei fStride;
95 GrGLvoid* fOffset;
96 };
97
98 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
99};
100
101/**
102 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
103 * and is used to track the state of the vertex array to avoid redundant GL calls.
104 */
105class GrGLVertexArray : public GrResource {
106public:
107 GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount);
108
109 /**
110 * Binds this vertex array. If the ID has been deleted or abandoned then NULL is returned.
111 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
112 * returned.
113 */
114 GrGLAttribArrayState* bind();
115
116 /**
117 * This is a version of the above function that also binds an index buffer to the vertex
118 * array object.
119 */
120 GrGLAttribArrayState* bindWithIndexBuffer(const GrGLIndexBuffer* indexBuffer);
121
122 void notifyIndexBufferDelete(GrGLuint bufferID);
123
124 void notifyVertexBufferDelete(GrGLuint id) {
125 fAttribArrays.notifyVertexBufferDelete(id);
126 }
127
128 GrGLuint arrayID() const { return fID; }
129
130 void invalidateCachedState();
131
132 virtual size_t sizeInBytes() const SK_OVERRIDE { return 0; }
133
134protected:
135 virtual void onAbandon() SK_OVERRIDE;
136
137 virtual void onRelease() SK_OVERRIDE;
138
139private:
140 GrGLuint fID;
141 GrGLAttribArrayState fAttribArrays;
142 GrGLuint fIndexBufferID;
143 bool fIndexBufferIDIsValid;
144
145 typedef GrResource INHERITED;
146};
147
148#endif