Line data Source code
1 : //
2 : // Copyright (c) 2002-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 : // angleutils.h: Common ANGLE utilities.
8 :
9 : #ifndef COMMON_ANGLEUTILS_H_
10 : #define COMMON_ANGLEUTILS_H_
11 :
12 : #include "common/platform.h"
13 :
14 : #include <climits>
15 : #include <cstdarg>
16 : #include <cstddef>
17 : #include <string>
18 : #include <set>
19 : #include <sstream>
20 : #include <vector>
21 :
22 : // A helper class to disallow copy and assignment operators
23 : namespace angle
24 : {
25 :
26 : #if defined(ANBLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
27 : using Microsoft::WRL::ComPtr;
28 : #endif // defined(ANBLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
29 :
30 : class NonCopyable
31 : {
32 : public:
33 0 : NonCopyable() = default;
34 : ~NonCopyable() = default;
35 : protected:
36 : NonCopyable(const NonCopyable&) = delete;
37 : void operator=(const NonCopyable&) = delete;
38 : };
39 :
40 : extern const uintptr_t DirtyPointer;
41 : } // namespace angle
42 :
43 : template <typename T, size_t N>
44 : constexpr inline size_t ArraySize(T (&)[N])
45 : {
46 : return N;
47 : }
48 :
49 : template <typename T, unsigned int N>
50 : void SafeRelease(T (&resourceBlock)[N])
51 : {
52 : for (unsigned int i = 0; i < N; i++)
53 : {
54 : SafeRelease(resourceBlock[i]);
55 : }
56 : }
57 :
58 : template <typename T>
59 : void SafeRelease(T& resource)
60 : {
61 : if (resource)
62 : {
63 : resource->Release();
64 : resource = NULL;
65 : }
66 : }
67 :
68 : template <typename T>
69 0 : void SafeDelete(T *&resource)
70 : {
71 0 : delete resource;
72 0 : resource = NULL;
73 0 : }
74 :
75 : template <typename T>
76 : void SafeDeleteContainer(T& resource)
77 : {
78 : for (auto &element : resource)
79 : {
80 : SafeDelete(element);
81 : }
82 : resource.clear();
83 : }
84 :
85 : template <typename T>
86 : void SafeDeleteArray(T*& resource)
87 : {
88 : delete[] resource;
89 : resource = NULL;
90 : }
91 :
92 : // Provide a less-than function for comparing structs
93 : // Note: struct memory must be initialized to zero, because of packing gaps
94 : template <typename T>
95 : inline bool StructLessThan(const T &a, const T &b)
96 : {
97 : return (memcmp(&a, &b, sizeof(T)) < 0);
98 : }
99 :
100 : // Provide a less-than function for comparing structs
101 : // Note: struct memory must be initialized to zero, because of packing gaps
102 : template <typename T>
103 : inline bool StructEquals(const T &a, const T &b)
104 : {
105 : return (memcmp(&a, &b, sizeof(T)) == 0);
106 : }
107 :
108 : template <typename T>
109 : inline void StructZero(T *obj)
110 : {
111 : memset(obj, 0, sizeof(T));
112 : }
113 :
114 : template <typename T>
115 : inline bool IsMaskFlagSet(T mask, T flag)
116 : {
117 : // Handles multibit flags as well
118 : return (mask & flag) == flag;
119 : }
120 :
121 : inline const char* MakeStaticString(const std::string &str)
122 : {
123 : static std::set<std::string> strings;
124 : std::set<std::string>::iterator it = strings.find(str);
125 : if (it != strings.end())
126 : {
127 : return it->c_str();
128 : }
129 :
130 : return strings.insert(str).first->c_str();
131 : }
132 :
133 0 : inline std::string ArrayString(unsigned int i)
134 : {
135 : // We assume UINT_MAX and GL_INVALID_INDEX are equal
136 : // See DynamicHLSL.cpp
137 0 : if (i == UINT_MAX)
138 : {
139 0 : return "";
140 : }
141 :
142 0 : std::stringstream strstr;
143 :
144 0 : strstr << "[";
145 0 : strstr << i;
146 0 : strstr << "]";
147 :
148 0 : return strstr.str();
149 : }
150 :
151 0 : inline std::string Str(int i)
152 : {
153 0 : std::stringstream strstr;
154 0 : strstr << i;
155 0 : return strstr.str();
156 : }
157 :
158 : size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
159 :
160 : std::string FormatString(const char *fmt, va_list vararg);
161 : std::string FormatString(const char *fmt, ...);
162 :
163 : template <typename T>
164 0 : std::string ToString(const T &value)
165 : {
166 0 : std::ostringstream o;
167 0 : o << value;
168 0 : return o.str();
169 : }
170 :
171 : // snprintf is not defined with MSVC prior to to msvc14
172 : #if defined(_MSC_VER) && _MSC_VER < 1900
173 : #define snprintf _snprintf
174 : #endif
175 :
176 : #define GL_BGR565_ANGLEX 0x6ABB
177 : #define GL_BGRA4_ANGLEX 0x6ABC
178 : #define GL_BGR5_A1_ANGLEX 0x6ABD
179 : #define GL_INT_64_ANGLEX 0x6ABE
180 : #define GL_STRUCT_ANGLEX 0x6ABF
181 :
182 : // Hidden enum for the NULL D3D device type.
183 : #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
184 :
185 : #define ANGLE_TRY_CHECKED_MATH(result) \
186 : if (!result.IsValid()) \
187 : { \
188 : return gl::Error(GL_INVALID_OPERATION, "Integer overflow."); \
189 : }
190 :
191 : #endif // COMMON_ANGLEUTILS_H_
|