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