• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include "libANGLE/AttributeMap.h"
8 
9 #include "common/debug.h"
10 
11 namespace egl
12 {
13 
14 AttributeMap::AttributeMap() = default;
15 
16 AttributeMap::AttributeMap(const AttributeMap &other) = default;
17 
18 AttributeMap &AttributeMap::operator=(const AttributeMap &other) = default;
19 
20 AttributeMap::~AttributeMap() = default;
21 
insert(EGLAttrib key,EGLAttrib value)22 void AttributeMap::insert(EGLAttrib key, EGLAttrib value)
23 {
24     mValidatedAttributes[key] = value;
25 }
26 
contains(EGLAttrib key) const27 bool AttributeMap::contains(EGLAttrib key) const
28 {
29     return (attribs().find(key) != attribs().end());
30 }
31 
get(EGLAttrib key) const32 EGLAttrib AttributeMap::get(EGLAttrib key) const
33 {
34     auto iter = attribs().find(key);
35     ASSERT(iter != attribs().end());
36     return iter->second;
37 }
38 
get(EGLAttrib key,EGLAttrib defaultValue) const39 EGLAttrib AttributeMap::get(EGLAttrib key, EGLAttrib defaultValue) const
40 {
41     auto iter = attribs().find(key);
42     return (iter != attribs().end()) ? iter->second : defaultValue;
43 }
44 
getAsInt(EGLAttrib key) const45 EGLint AttributeMap::getAsInt(EGLAttrib key) const
46 {
47     return static_cast<EGLint>(get(key));
48 }
49 
getAsInt(EGLAttrib key,EGLint defaultValue) const50 EGLint AttributeMap::getAsInt(EGLAttrib key, EGLint defaultValue) const
51 {
52     return static_cast<EGLint>(get(key, static_cast<EGLAttrib>(defaultValue)));
53 }
54 
isEmpty() const55 bool AttributeMap::isEmpty() const
56 {
57     return attribs().empty();
58 }
59 
toIntVector() const60 std::vector<EGLint> AttributeMap::toIntVector() const
61 {
62     std::vector<EGLint> ret;
63     for (const auto &pair : attribs())
64     {
65         ret.push_back(static_cast<EGLint>(pair.first));
66         ret.push_back(static_cast<EGLint>(pair.second));
67     }
68     ret.push_back(EGL_NONE);
69 
70     return ret;
71 }
72 
begin() const73 AttributeMap::const_iterator AttributeMap::begin() const
74 {
75     return attribs().begin();
76 }
77 
end() const78 AttributeMap::const_iterator AttributeMap::end() const
79 {
80     return attribs().end();
81 }
82 
validate(const ValidationContext * val,const egl::Display * display,AttributeValidationFunc validationFunc) const83 bool AttributeMap::validate(const ValidationContext *val,
84                             const egl::Display *display,
85                             AttributeValidationFunc validationFunc) const
86 {
87     if (mIntPointer)
88     {
89         for (const EGLint *curAttrib = mIntPointer; curAttrib[0] != EGL_NONE; curAttrib += 2)
90         {
91             if (!validationFunc(val, display, curAttrib[0]))
92             {
93                 return false;
94             }
95 
96             mValidatedAttributes[static_cast<EGLAttrib>(curAttrib[0])] =
97                 static_cast<EGLAttrib>(curAttrib[1]);
98         }
99         mIntPointer = nullptr;
100     }
101 
102     if (mAttribPointer)
103     {
104         for (const EGLAttrib *curAttrib = mAttribPointer; curAttrib[0] != EGL_NONE; curAttrib += 2)
105         {
106             if (!validationFunc(val, display, curAttrib[0]))
107             {
108                 return false;
109             }
110 
111             mValidatedAttributes[curAttrib[0]] = curAttrib[1];
112         }
113         mAttribPointer = nullptr;
114     }
115 
116     return true;
117 }
118 
initializeWithoutValidation() const119 void AttributeMap::initializeWithoutValidation() const
120 {
121     auto alwaysTrue = [](const ValidationContext *, const egl::Display *, EGLAttrib) {
122         return true;
123     };
124     (void)validate(nullptr, nullptr, alwaysTrue);
125 }
126 
127 // static
CreateFromIntArray(const EGLint * attributes)128 AttributeMap AttributeMap::CreateFromIntArray(const EGLint *attributes)
129 {
130     AttributeMap map;
131     map.mIntPointer = attributes;
132     return map;
133 }
134 
135 // static
CreateFromAttribArray(const EGLAttrib * attributes)136 AttributeMap AttributeMap::CreateFromAttribArray(const EGLAttrib *attributes)
137 {
138     AttributeMap map;
139     map.mAttribPointer = attributes;
140     return map;
141 }
142 
isValidated() const143 bool AttributeMap::isValidated() const
144 {
145     return mIntPointer == nullptr && mAttribPointer == nullptr;
146 }
147 }  // namespace egl
148