1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 // This is almost literally
18 // external/angle/src/common/mathutil.h: IndexRange +
19 // external/angle/src/libANGLE/IndexRangeCache.h: IndexRangeCache,
20 // with adaptations to work with goldfish opengl driver.
21 // Currently, primitive restart is not supported, so there
22 // is a very minimal incorporation of that.
23 
24 #ifndef _GL_INDEX_RANGE_CACHE_H_
25 #define _GL_INDEX_RANGE_CACHE_H_
26 
27 #include <GLES/gl.h>
28 #include <GLES/glext.h>
29 #include <GLES2/gl2.h>
30 #include <GLES2/gl2ext.h>
31 
32 #include "glUtils.h"
33 
34 #include <map>
35 
36 struct IndexRange {
37     // Inclusive range of indices that are not primitive restart
38     int start;
39     int end;
40 
41     // Number of non-primitive restart indices
42     size_t vertexIndexCount; // TODO; not being accounted yet (GLES3 feature)
43 };
44 
45 class IndexRangeCache {
46 public:
47     void addRange(GLenum type,
48                   size_t offset,
49                   size_t count,
50                   bool primitiveRestartEnabled,
51                   int start,
52                   int end);
53     bool findRange(GLenum type,
54                    size_t offset,
55                    size_t count,
56                    bool primitiveRestartEnabled,
57                    int* start_out,
58                    int* end_out) const;
59     void invalidateRange(size_t offset, size_t size);
60     void clear();
61 private:
62     struct IndexRangeKey {
IndexRangeKeyIndexRangeKey63         IndexRangeKey() :
64             type(GL_NONE),
65             offset(0),
66             count(0),
67             primitiveRestartEnabled(false) { }
IndexRangeKeyIndexRangeKey68         IndexRangeKey(GLenum _type,
69                       size_t _offset,
70                       size_t _count,
71                       bool _primitiveRestart) :
72             type(_type),
73             offset(_offset),
74             count(_count),
75             primitiveRestartEnabled(_primitiveRestart) { }
76 
77         bool operator<(const IndexRangeKey& rhs) const {
78             size_t start = offset;
79             size_t start_other = rhs.offset;
80             size_t end = offset + count * glSizeof(type);
81             size_t end_other = rhs.offset + rhs.count * glSizeof(rhs.type);
82 
83             if (end <= start_other) {
84                 return true;
85             }
86 
87             if (type != rhs.type) return type < rhs.type;
88             if (count != rhs.count) return count < rhs.count;
89             if (primitiveRestartEnabled != rhs.primitiveRestartEnabled)
90                 return primitiveRestartEnabled;
91             return false;
92         }
93 
94         GLenum type;
95         size_t offset;
96         size_t count;
97         bool primitiveRestartEnabled;
98     };
99 
100     typedef std::map<IndexRangeKey, IndexRange> IndexRangeMap;
101     IndexRangeMap mIndexRangeCache;
102 };
103 
104 #endif
105