daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 9 | #ifndef COMMON_ANGLEUTILS_H_ |
| 10 | #define COMMON_ANGLEUTILS_H_ |
| 11 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 12 | #include "common/platform.h" |
| 13 | |
Jamie Madill | 55573e1 | 2014-11-25 11:21:13 -0500 | [diff] [blame] | 14 | #include <climits> |
| 15 | #include <cstdarg> |
| 16 | #include <cstddef> |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <set> |
Jamie Madill | 53cb14d | 2014-07-08 15:02:35 -0400 | [diff] [blame] | 19 | #include <sstream> |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 20 | #include <vector> |
Jamie Madill | f386bf7 | 2013-07-08 14:02:41 -0400 | [diff] [blame] | 21 | |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 22 | // A helper class to disallow copy and assignment operators |
| 23 | namespace angle |
| 24 | { |
| 25 | |
Brian Osman | 63d8262 | 2017-01-06 14:05:18 -0500 | [diff] [blame] | 26 | #if defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11) |
Jamie Madill | 47c0e04 | 2016-11-30 13:44:45 -0500 | [diff] [blame] | 27 | using Microsoft::WRL::ComPtr; |
Brian Osman | 63d8262 | 2017-01-06 14:05:18 -0500 | [diff] [blame] | 28 | #endif // defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11) |
Jamie Madill | 47c0e04 | 2016-11-30 13:44:45 -0500 | [diff] [blame] | 29 | |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 30 | class NonCopyable |
| 31 | { |
Frank Henigman | aa7203e | 2017-05-03 23:32:29 -0400 | [diff] [blame] | 32 | protected: |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 33 | NonCopyable() = default; |
| 34 | ~NonCopyable() = default; |
Frank Henigman | aa7203e | 2017-05-03 23:32:29 -0400 | [diff] [blame] | 35 | |
| 36 | private: |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 37 | NonCopyable(const NonCopyable&) = delete; |
| 38 | void operator=(const NonCopyable&) = delete; |
| 39 | }; |
| 40 | |
Jamie Madill | c9bdeff | 2016-02-08 12:36:55 -0500 | [diff] [blame] | 41 | extern const uintptr_t DirtyPointer; |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 42 | |
| 43 | // Helper class for wrapping an onDestroy function. |
| 44 | template <typename ObjT, typename ContextT> |
| 45 | class UniqueObjectPointer : angle::NonCopyable |
| 46 | { |
| 47 | public: |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 48 | UniqueObjectPointer(const ContextT *context) : mObject(nullptr), mContext(context) {} |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 49 | UniqueObjectPointer(ObjT *obj, const ContextT *context) : mObject(obj), mContext(context) {} |
| 50 | ~UniqueObjectPointer() |
| 51 | { |
| 52 | if (mObject) |
| 53 | { |
| 54 | mObject->onDestroy(mContext); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | ObjT *operator->() const { return mObject; } |
| 59 | |
| 60 | ObjT *release() |
| 61 | { |
| 62 | auto obj = mObject; |
| 63 | mObject = nullptr; |
| 64 | return obj; |
| 65 | } |
| 66 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 67 | ObjT *get() const { return mObject; } |
| 68 | |
| 69 | void reset(ObjT *obj) |
| 70 | { |
| 71 | if (mObject) |
| 72 | { |
| 73 | mObject->onDestroy(mContext); |
| 74 | } |
| 75 | mObject = obj; |
| 76 | } |
| 77 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 78 | private: |
| 79 | ObjT *mObject; |
| 80 | const ContextT *mContext; |
| 81 | }; |
| 82 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 83 | } // namespace angle |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 84 | |
Jamie Madill | 33ea2f9 | 2014-08-29 15:15:01 -0400 | [diff] [blame] | 85 | template <typename T, size_t N> |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 86 | constexpr inline size_t ArraySize(T (&)[N]) |
shannon.woods@transgaming.com | 6d79257 | 2013-02-28 23:07:25 +0000 | [diff] [blame] | 87 | { |
| 88 | return N; |
| 89 | } |
| 90 | |
Jamie Madill | 5978e28 | 2017-06-02 11:49:31 -0400 | [diff] [blame] | 91 | template <typename T> |
| 92 | class WrappedArray final : angle::NonCopyable |
| 93 | { |
| 94 | public: |
| 95 | template <size_t N> |
| 96 | constexpr WrappedArray(const T (&data)[N]) : mArray(&data[0]), mSize(N) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | constexpr WrappedArray() : mArray(nullptr), mSize(0) {} |
| 101 | constexpr WrappedArray(const T *data, size_t size) : mArray(data), mSize(size) {} |
Jamie Madill | 92996b0 | 2017-08-17 10:39:02 -0400 | [diff] [blame] | 102 | |
Jamie Madill | 2c1183b | 2017-08-24 10:36:01 -0700 | [diff] [blame] | 103 | WrappedArray(WrappedArray &&other) : WrappedArray() |
Jamie Madill | 92996b0 | 2017-08-17 10:39:02 -0400 | [diff] [blame] | 104 | { |
| 105 | std::swap(mArray, other.mArray); |
| 106 | std::swap(mSize, other.mSize); |
| 107 | } |
| 108 | |
Jamie Madill | 5978e28 | 2017-06-02 11:49:31 -0400 | [diff] [blame] | 109 | ~WrappedArray() {} |
| 110 | |
| 111 | constexpr const T *get() const { return mArray; } |
| 112 | constexpr size_t size() const { return mSize; } |
| 113 | |
| 114 | private: |
| 115 | const T *mArray; |
| 116 | size_t mSize; |
| 117 | }; |
| 118 | |
shannon.woods%transgaming.com@gtempaccount.com | 9622477 | 2013-04-13 03:30:18 +0000 | [diff] [blame] | 119 | template <typename T, unsigned int N> |
| 120 | void SafeRelease(T (&resourceBlock)[N]) |
| 121 | { |
| 122 | for (unsigned int i = 0; i < N; i++) |
| 123 | { |
| 124 | SafeRelease(resourceBlock[i]); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | template <typename T> |
| 129 | void SafeRelease(T& resource) |
| 130 | { |
| 131 | if (resource) |
| 132 | { |
| 133 | resource->Release(); |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 134 | resource = nullptr; |
shannon.woods%transgaming.com@gtempaccount.com | 9622477 | 2013-04-13 03:30:18 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 138 | template <typename T> |
Jamie Madill | 78a9c73 | 2016-07-15 11:22:43 -0400 | [diff] [blame] | 139 | void SafeDelete(T *&resource) |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 140 | { |
| 141 | delete resource; |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 142 | resource = nullptr; |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | template <typename T> |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 146 | void SafeDeleteContainer(T& resource) |
| 147 | { |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 148 | for (auto &element : resource) |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 149 | { |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 150 | SafeDelete(element); |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 151 | } |
| 152 | resource.clear(); |
| 153 | } |
| 154 | |
| 155 | template <typename T> |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 156 | void SafeDeleteArray(T*& resource) |
| 157 | { |
| 158 | delete[] resource; |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 159 | resource = nullptr; |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Jamie Madill | d3f0f1e | 2013-09-20 13:31:08 -0400 | [diff] [blame] | 162 | // Provide a less-than function for comparing structs |
| 163 | // Note: struct memory must be initialized to zero, because of packing gaps |
| 164 | template <typename T> |
| 165 | inline bool StructLessThan(const T &a, const T &b) |
| 166 | { |
| 167 | return (memcmp(&a, &b, sizeof(T)) < 0); |
| 168 | } |
| 169 | |
| 170 | // Provide a less-than function for comparing structs |
| 171 | // Note: struct memory must be initialized to zero, because of packing gaps |
| 172 | template <typename T> |
| 173 | inline bool StructEquals(const T &a, const T &b) |
| 174 | { |
| 175 | return (memcmp(&a, &b, sizeof(T)) == 0); |
| 176 | } |
| 177 | |
| 178 | template <typename T> |
| 179 | inline void StructZero(T *obj) |
| 180 | { |
| 181 | memset(obj, 0, sizeof(T)); |
| 182 | } |
| 183 | |
Tibor den Ouden | 2221f47 | 2014-10-22 15:07:05 +0200 | [diff] [blame] | 184 | template <typename T> |
| 185 | inline bool IsMaskFlagSet(T mask, T flag) |
| 186 | { |
| 187 | // Handles multibit flags as well |
| 188 | return (mask & flag) == flag; |
| 189 | } |
| 190 | |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 191 | inline const char* MakeStaticString(const std::string &str) |
| 192 | { |
| 193 | static std::set<std::string> strings; |
| 194 | std::set<std::string>::iterator it = strings.find(str); |
| 195 | if (it != strings.end()) |
| 196 | { |
| 197 | return it->c_str(); |
| 198 | } |
| 199 | |
| 200 | return strings.insert(str).first->c_str(); |
| 201 | } |
| 202 | |
Jamie Madill | 53cb14d | 2014-07-08 15:02:35 -0400 | [diff] [blame] | 203 | inline std::string ArrayString(unsigned int i) |
| 204 | { |
| 205 | // We assume UINT_MAX and GL_INVALID_INDEX are equal |
| 206 | // See DynamicHLSL.cpp |
| 207 | if (i == UINT_MAX) |
| 208 | { |
| 209 | return ""; |
| 210 | } |
| 211 | |
| 212 | std::stringstream strstr; |
| 213 | |
| 214 | strstr << "["; |
| 215 | strstr << i; |
| 216 | strstr << "]"; |
| 217 | |
| 218 | return strstr.str(); |
| 219 | } |
| 220 | |
| 221 | inline std::string Str(int i) |
| 222 | { |
| 223 | std::stringstream strstr; |
| 224 | strstr << i; |
| 225 | return strstr.str(); |
| 226 | } |
| 227 | |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 228 | size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer); |
| 229 | |
Shannon Woods | 8e7d7a3 | 2014-09-02 17:09:08 -0400 | [diff] [blame] | 230 | std::string FormatString(const char *fmt, va_list vararg); |
| 231 | std::string FormatString(const char *fmt, ...); |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 232 | |
Corentin Wallez | 054f7ed | 2016-09-20 17:15:59 -0400 | [diff] [blame] | 233 | template <typename T> |
| 234 | std::string ToString(const T &value) |
| 235 | { |
| 236 | std::ostringstream o; |
| 237 | o << value; |
| 238 | return o.str(); |
| 239 | } |
| 240 | |
Geoff Lang | df8fafe | 2014-11-11 11:11:33 -0500 | [diff] [blame] | 241 | // snprintf is not defined with MSVC prior to to msvc14 |
| 242 | #if defined(_MSC_VER) && _MSC_VER < 1900 |
alokp@chromium.org | 79fb101 | 2012-04-26 21:07:39 +0000 | [diff] [blame] | 243 | #define snprintf _snprintf |
| 244 | #endif |
| 245 | |
Geoff Lang | aadc8f3 | 2017-08-11 17:34:44 -0400 | [diff] [blame] | 246 | #define GL_BGRX8_ANGLEX 0x6ABA |
Jamie Madill | ec0b580 | 2016-07-04 13:11:59 -0400 | [diff] [blame] | 247 | #define GL_BGR565_ANGLEX 0x6ABB |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 248 | #define GL_BGRA4_ANGLEX 0x6ABC |
| 249 | #define GL_BGR5_A1_ANGLEX 0x6ABD |
Jamie Madill | 0fda986 | 2013-07-19 16:36:55 -0400 | [diff] [blame] | 250 | #define GL_INT_64_ANGLEX 0x6ABE |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 251 | |
Geoff Lang | 7825f61 | 2014-11-26 16:19:41 -0500 | [diff] [blame] | 252 | // Hidden enum for the NULL D3D device type. |
| 253 | #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0 |
| 254 | |
Jamie Madill | 98de826 | 2017-05-29 13:01:02 -0400 | [diff] [blame] | 255 | // TODO(jmadill): Clean this up at some point. |
| 256 | #define EGL_PLATFORM_ANGLE_PLATFORM_METHODS_ANGLEX 0x9999 |
| 257 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 258 | #define ANGLE_TRY_CHECKED_MATH(result) \ |
| 259 | if (!result.IsValid()) \ |
| 260 | { \ |
| 261 | return gl::InternalError() << "Integer overflow."; \ |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 262 | } |
| 263 | |
Jamie Madill | 44183cc | 2017-08-01 12:48:34 -0400 | [diff] [blame] | 264 | // The below inlining code lifted from V8. |
jchen10 | 3c76d59 | 2017-08-03 08:47:56 +0800 | [diff] [blame] | 265 | #if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute)) |
Jamie Madill | 44183cc | 2017-08-01 12:48:34 -0400 | [diff] [blame] | 266 | #define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE (__has_attribute(always_inline)) |
| 267 | #define ANGLE_HAS___FORCEINLINE 0 |
| 268 | #elif defined(_MSC_VER) |
| 269 | #define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE 0 |
| 270 | #define ANGLE_HAS___FORCEINLINE 1 |
| 271 | #else |
| 272 | #define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE 0 |
| 273 | #define ANGLE_HAS___FORCEINLINE 0 |
| 274 | #endif |
| 275 | |
| 276 | #if defined(NDEBUG) && ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE |
| 277 | #define ANGLE_INLINE inline __attribute__((always_inline)) |
| 278 | #elif defined(NDEBUG) && ANGLE_HAS___FORCEINLINE |
| 279 | #define ANGLE_INLINE __forceinline |
| 280 | #else |
| 281 | #define ANGLE_INLINE inline |
| 282 | #endif |
| 283 | |
Jamie Madill | 92996b0 | 2017-08-17 10:39:02 -0400 | [diff] [blame] | 284 | #ifndef ANGLE_STRINGIFY |
| 285 | #define ANGLE_STRINGIFY(x) #x |
| 286 | #endif |
| 287 | |
| 288 | #ifndef ANGLE_MACRO_STRINGIFY |
| 289 | #define ANGLE_MACRO_STRINGIFY(x) ANGLE_STRINGIFY(x) |
| 290 | #endif |
| 291 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 292 | #endif // COMMON_ANGLEUTILS_H_ |