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/FastVector.h" 11 #include "common/PackedEnums.h" 12 13 #include <EGL/egl.h> 14 15 #include <functional> 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 enum AttributeMapType 28 { 29 Invalid, 30 Attrib, 31 Int, 32 }; 33 34 class AttributeMap final 35 { 36 public: 37 static constexpr size_t kMapSize = 2; 38 using Map = angle::FlatUnorderedMap<EGLAttrib, EGLAttrib, kMapSize>; 39 40 AttributeMap(); 41 AttributeMap(const AttributeMap &other); 42 AttributeMap &operator=(const AttributeMap &other); 43 ~AttributeMap(); 44 45 void insert(EGLAttrib key, EGLAttrib value); 46 bool contains(EGLAttrib key) const; 47 48 EGLAttrib get(EGLAttrib key) const; 49 EGLAttrib get(EGLAttrib key, EGLAttrib defaultValue) const; 50 EGLint getAsInt(EGLAttrib key) const; 51 EGLint getAsInt(EGLAttrib key, EGLint defaultValue) const; 52 53 template <typename PackedEnumT> getAsPackedEnum(EGLAttrib key)54 PackedEnumT getAsPackedEnum(EGLAttrib key) const 55 { 56 return FromEGLenum<PackedEnumT>(static_cast<EGLenum>(get(key))); 57 } 58 59 using const_iterator = Map::const_iterator; 60 61 template <typename PackedEnumT> getAsPackedEnum(EGLAttrib key,PackedEnumT defaultValue)62 PackedEnumT getAsPackedEnum(EGLAttrib key, PackedEnumT defaultValue) const 63 { 64 const_iterator iter = attribs().find(key); 65 return (attribs().find(key) != attribs().end()) 66 ? FromEGLenum<PackedEnumT>(static_cast<EGLenum>(iter->second)) 67 : defaultValue; 68 } 69 70 bool isEmpty() const; 71 std::vector<EGLint> toIntVector() const; 72 73 const_iterator begin() const; 74 const_iterator end() const; 75 76 [[nodiscard]] bool validate(const ValidationContext *val, 77 const egl::Display *display, 78 AttributeValidationFunc validationFunc) const; 79 80 // TODO: remove this and validate at every call site. http://anglebug.com/42265167 81 void initializeWithoutValidation() const; 82 83 static AttributeMap CreateFromIntArray(const EGLint *attributes); 84 static AttributeMap CreateFromAttribArray(const EGLAttrib *attributes); 85 getType()86 AttributeMapType getType() const { return mMapType; } 87 88 private: 89 bool isValidated() const; 90 attribs()91 const Map &attribs() const 92 { 93 ASSERT(isValidated()); 94 return mValidatedAttributes; 95 } 96 attribs()97 Map &attribs() 98 { 99 ASSERT(isValidated()); 100 return mValidatedAttributes; 101 } 102 103 mutable const EGLint *mIntPointer = nullptr; 104 mutable const EGLAttrib *mAttribPointer = nullptr; 105 mutable Map mValidatedAttributes; 106 mutable AttributeMapType mMapType = AttributeMapType::Invalid; 107 }; 108 } // namespace egl 109 110 #endif // LIBANGLE_ATTRIBUTEMAP_H_ 111