1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef GPU_COMMAND_BUFFER_CLIENT_VERTEX_ARRAY_OBJECT_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_VERTEX_ARRAY_OBJECT_MANAGER_H_
7 
8 #include <GLES2/gl2.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <unordered_map>
13 
14 #include "base/macros.h"
15 #include "gles2_impl_export.h"
16 
17 namespace gpu {
18 namespace gles2 {
19 
20 class GLES2Implementation;
21 class GLES2CmdHelper;
22 class VertexArrayObject;
23 
24 // VertexArrayObjectManager manages vertex array objects on the client side
25 // of the command buffer.
26 class GLES2_IMPL_EXPORT VertexArrayObjectManager {
27  public:
28   VertexArrayObjectManager(
29       GLuint max_vertex_attribs,
30       GLuint array_buffer_id,
31       GLuint element_array_buffer_id,
32       bool support_client_side_arrays);
33   ~VertexArrayObjectManager();
34 
35   bool IsReservedId(GLuint id) const;
36 
37   // Binds an element array.
38   // Returns true if service should be called.
39   bool BindElementArray(GLuint id);
40 
41   // Unbind buffer.
42   void UnbindBuffer(GLuint id);
43 
44   // Geneates array objects for the given ids.
45   void GenVertexArrays(GLsizei n, const GLuint* arrays);
46 
47   // Deletes array objects for the given ids.
48   void DeleteVertexArrays(GLsizei n, const GLuint* arrays);
49 
50   // Binds a vertex array.
51   // changed will be set to true if the service should be called.
52   // Returns false if array is an unknown id.
53   bool BindVertexArray(GLuint array, bool* changed);
54 
55   // Whether client side buffers are supported.
56   bool SupportsClientSideBuffers();
57 
58   // simulated will be set to true if buffers were simulated.
59   // Returns true service should be called.
60   bool SetupSimulatedClientSideBuffers(
61       const char* function_name,
62       GLES2Implementation* gl,
63       GLES2CmdHelper* gl_helper,
64       GLsizei num_elements,
65       GLsizei primcount,
66       bool* simulated);
67 
68   // Returns true if buffers were setup.
69   bool SetupSimulatedIndexAndClientSideBuffers(
70       const char* function_name,
71       GLES2Implementation* gl,
72       GLES2CmdHelper* gl_helper,
73       GLsizei count,
74       GLenum type,
75       GLsizei primcount,
76       const void* indices,
77       GLuint* offset,
78       bool* simulated);
79 
80   bool HaveEnabledClientSideBuffers() const;
81 
82   void SetAttribEnable(GLuint index, bool enabled);
83 
84   bool GetVertexAttrib(GLuint index, GLenum pname, uint32_t* param);
85 
86   bool GetAttribPointer(GLuint index, GLenum pname, void** ptr) const;
87 
88   // Returns false if error.
89   bool SetAttribPointer(
90       GLuint buffer_id,
91       GLuint index,
92       GLint size,
93       GLenum type,
94       GLboolean normalized,
95       GLsizei stride,
96       const void* ptr,
97       GLboolean integer);
98 
99   void SetAttribDivisor(GLuint index, GLuint divisor);
100 
101   GLuint bound_element_array_buffer() const;
102 
103  private:
104   typedef std::unordered_map<GLuint, VertexArrayObject*> VertexArrayObjectMap;
105 
106   bool IsDefaultVAOBound() const;
107 
108   GLsizei CollectData(const void* data,
109                       GLsizei bytes_per_element,
110                       GLsizei real_stride,
111                       GLsizei num_elements);
112 
113   GLuint max_vertex_attribs_;
114   GLuint array_buffer_id_;
115   GLsizei array_buffer_size_;
116   GLsizei array_buffer_offset_;
117   GLuint element_array_buffer_id_;
118   GLsizei element_array_buffer_size_;
119   GLsizei collection_buffer_size_;
120   std::unique_ptr<int8_t[]> collection_buffer_;
121 
122   VertexArrayObject* default_vertex_array_object_;
123   VertexArrayObject* bound_vertex_array_object_;
124   VertexArrayObjectMap vertex_array_objects_;
125 
126   const bool support_client_side_arrays_;
127 
128   DISALLOW_COPY_AND_ASSIGN(VertexArrayObjectManager);
129 };
130 
131 }  // namespace gles2
132 }  // namespace gpu
133 
134 #endif  // GPU_COMMAND_BUFFER_CLIENT_VERTEX_ARRAY_OBJECT_MANAGER_H_
135 
136