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 #ifndef LIBANGLE_ATTRIBUTEMAP_H_ 8 #define LIBANGLE_ATTRIBUTEMAP_H_ 9 10 #include "common/PackedEnums.h" 11 12 #include <EGL/egl.h> 13 14 #include <functional> 15 #include <map> 16 #include <vector> 17 18 namespace egl 19 { 20 class Display; 21 struct ValidationContext; 22 23 // Validates {key, value} for each attribute. Generates an error and returns false on invalid usage. 24 using AttributeValidationFunc = 25 std::function<bool(const ValidationContext *, const Display *, EGLAttrib)>; 26 27 class AttributeMap final 28 { 29 public: 30 AttributeMap(); 31 AttributeMap(const AttributeMap &other); 32 AttributeMap &operator=(const AttributeMap &other); 33 ~AttributeMap(); 34 35 void insert(EGLAttrib key, EGLAttrib value); 36 bool contains(EGLAttrib key) const; 37 38 EGLAttrib get(EGLAttrib key) const; 39 EGLAttrib get(EGLAttrib key, EGLAttrib defaultValue) const; 40 EGLint getAsInt(EGLAttrib key) const; 41 EGLint getAsInt(EGLAttrib key, EGLint defaultValue) const; 42 43 template <typename PackedEnumT> getAsPackedEnum(EGLAttrib key)44 PackedEnumT getAsPackedEnum(EGLAttrib key) const 45 { 46 return FromEGLenum<PackedEnumT>(static_cast<EGLenum>(get(key))); 47 } 48 49 template <typename PackedEnumT> getAsPackedEnum(EGLAttrib key,PackedEnumT defaultValue)50 PackedEnumT getAsPackedEnum(EGLAttrib key, PackedEnumT defaultValue) const 51 { 52 auto iter = attribs().find(key); 53 return (attribs().find(key) != attribs().end()) 54 ? FromEGLenum<PackedEnumT>(static_cast<EGLenum>(iter->second)) 55 : defaultValue; 56 } 57 58 bool isEmpty() const; 59 std::vector<EGLint> toIntVector() const; 60 61 typedef std::map<EGLAttrib, EGLAttrib>::const_iterator const_iterator; 62 63 const_iterator begin() const; 64 const_iterator end() const; 65 66 ANGLE_NO_DISCARD bool validate(const ValidationContext *val, 67 const egl::Display *display, 68 AttributeValidationFunc validationFunc) const; 69 70 // TODO: remove this and validate at every call site. http://anglebug.com/6671 71 void initializeWithoutValidation() const; 72 73 static AttributeMap CreateFromIntArray(const EGLint *attributes); 74 static AttributeMap CreateFromAttribArray(const EGLAttrib *attributes); 75 76 private: 77 bool isValidated() const; 78 attribs()79 const std::map<EGLAttrib, EGLAttrib> &attribs() const 80 { 81 ASSERT(isValidated()); 82 return mValidatedAttributes; 83 } 84 attribs()85 std::map<EGLAttrib, EGLAttrib> &attribs() 86 { 87 ASSERT(isValidated()); 88 return mValidatedAttributes; 89 } 90 91 mutable const EGLint *mIntPointer = nullptr; 92 mutable const EGLAttrib *mAttribPointer = nullptr; 93 mutable std::map<EGLAttrib, EGLAttrib> mValidatedAttributes; 94 }; 95 } // namespace egl 96 97 #endif // LIBANGLE_ATTRIBUTEMAP_H_ 98