blob: bd2879465d363c209545fc682ea29ffb925d32b0 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Langcec35902014-04-16 10:52:36 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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.combbf56f72010-04-20 18:52:13 +00009#ifndef COMMON_ANGLEUTILS_H_
10#define COMMON_ANGLEUTILS_H_
11
Geoff Lang44fa7592014-05-30 11:50:07 -040012#include "common/platform.h"
13
Jamie Madill55573e12014-11-25 11:21:13 -050014#include <climits>
15#include <cstdarg>
16#include <cstddef>
Geoff Langcec35902014-04-16 10:52:36 -040017#include <string>
18#include <set>
Jamie Madill53cb14d2014-07-08 15:02:35 -040019#include <sstream>
Austin Kinross922a9fb2014-10-21 14:26:33 -070020#include <vector>
Jamie Madillf386bf72013-07-08 14:02:41 -040021
Jamie Madillf0d10f82015-03-31 12:56:52 -040022// A helper class to disallow copy and assignment operators
23namespace angle
24{
25
Brian Osman63d82622017-01-06 14:05:18 -050026#if defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
Jamie Madill47c0e042016-11-30 13:44:45 -050027using Microsoft::WRL::ComPtr;
Brian Osman63d82622017-01-06 14:05:18 -050028#endif // defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
Jamie Madill47c0e042016-11-30 13:44:45 -050029
Jamie Madillf0d10f82015-03-31 12:56:52 -040030class NonCopyable
31{
Frank Henigmanaa7203e2017-05-03 23:32:29 -040032 protected:
Jamie Madillf0d10f82015-03-31 12:56:52 -040033 NonCopyable() = default;
34 ~NonCopyable() = default;
Frank Henigmanaa7203e2017-05-03 23:32:29 -040035
36 private:
Jamie Madillf0d10f82015-03-31 12:56:52 -040037 NonCopyable(const NonCopyable&) = delete;
38 void operator=(const NonCopyable&) = delete;
39};
40
Jamie Madillc9bdeff2016-02-08 12:36:55 -050041extern const uintptr_t DirtyPointer;
Jamie Madillfe548342017-06-19 11:13:24 -040042
Sami Väisänen46eaa942016-06-29 10:26:37 +030043} // namespace angle
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
Jamie Madill33ea2f92014-08-29 15:15:01 -040045template <typename T, size_t N>
Sami Väisänen46eaa942016-06-29 10:26:37 +030046constexpr inline size_t ArraySize(T (&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000047{
48 return N;
49}
50
Jamie Madill5978e282017-06-02 11:49:31 -040051template <typename T>
52class WrappedArray final : angle::NonCopyable
53{
54 public:
55 template <size_t N>
56 constexpr WrappedArray(const T (&data)[N]) : mArray(&data[0]), mSize(N)
57 {
58 }
59
60 constexpr WrappedArray() : mArray(nullptr), mSize(0) {}
61 constexpr WrappedArray(const T *data, size_t size) : mArray(data), mSize(size) {}
Jamie Madill92996b02017-08-17 10:39:02 -040062
Jamie Madill2c1183b2017-08-24 10:36:01 -070063 WrappedArray(WrappedArray &&other) : WrappedArray()
Jamie Madill92996b02017-08-17 10:39:02 -040064 {
65 std::swap(mArray, other.mArray);
66 std::swap(mSize, other.mSize);
67 }
68
Jamie Madill5978e282017-06-02 11:49:31 -040069 ~WrappedArray() {}
70
71 constexpr const T *get() const { return mArray; }
72 constexpr size_t size() const { return mSize; }
73
74 private:
75 const T *mArray;
76 size_t mSize;
77};
78
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000079template <typename T, unsigned int N>
80void SafeRelease(T (&resourceBlock)[N])
81{
82 for (unsigned int i = 0; i < N; i++)
83 {
84 SafeRelease(resourceBlock[i]);
85 }
86}
87
88template <typename T>
89void SafeRelease(T& resource)
90{
91 if (resource)
92 {
93 resource->Release();
Yunchao Hed7297bf2017-04-19 15:27:10 +080094 resource = nullptr;
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000095 }
96}
97
Geoff Langea228632013-07-30 15:17:12 -040098template <typename T>
Jamie Madill78a9c732016-07-15 11:22:43 -040099void SafeDelete(T *&resource)
Geoff Langea228632013-07-30 15:17:12 -0400100{
101 delete resource;
Yunchao Hed7297bf2017-04-19 15:27:10 +0800102 resource = nullptr;
Geoff Langea228632013-07-30 15:17:12 -0400103}
104
105template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -0400106void SafeDeleteContainer(T& resource)
107{
Jamie Madill4e31ad52015-10-29 10:32:57 -0400108 for (auto &element : resource)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400109 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400110 SafeDelete(element);
Geoff Lang04fb89a2014-06-09 15:05:36 -0400111 }
112 resource.clear();
113}
114
115template <typename T>
Geoff Langea228632013-07-30 15:17:12 -0400116void SafeDeleteArray(T*& resource)
117{
118 delete[] resource;
Yunchao Hed7297bf2017-04-19 15:27:10 +0800119 resource = nullptr;
Geoff Langea228632013-07-30 15:17:12 -0400120}
121
Jamie Madilld3f0f1e2013-09-20 13:31:08 -0400122// Provide a less-than function for comparing structs
123// Note: struct memory must be initialized to zero, because of packing gaps
124template <typename T>
125inline bool StructLessThan(const T &a, const T &b)
126{
127 return (memcmp(&a, &b, sizeof(T)) < 0);
128}
129
130// Provide a less-than function for comparing structs
131// Note: struct memory must be initialized to zero, because of packing gaps
132template <typename T>
133inline bool StructEquals(const T &a, const T &b)
134{
135 return (memcmp(&a, &b, sizeof(T)) == 0);
136}
137
138template <typename T>
139inline void StructZero(T *obj)
140{
141 memset(obj, 0, sizeof(T));
142}
143
Tibor den Ouden2221f472014-10-22 15:07:05 +0200144template <typename T>
145inline bool IsMaskFlagSet(T mask, T flag)
146{
147 // Handles multibit flags as well
148 return (mask & flag) == flag;
149}
150
Geoff Langcec35902014-04-16 10:52:36 -0400151inline const char* MakeStaticString(const std::string &str)
152{
153 static std::set<std::string> strings;
154 std::set<std::string>::iterator it = strings.find(str);
155 if (it != strings.end())
156 {
157 return it->c_str();
158 }
159
160 return strings.insert(str).first->c_str();
161}
162
Olli Etuahoc8538042017-09-27 11:20:15 +0300163std::string ArrayString(unsigned int i);
Jamie Madill53cb14d2014-07-08 15:02:35 -0400164
Olli Etuahoc8538042017-09-27 11:20:15 +0300165// Indices are stored in vectors with the outermost index in the back. In the output of the function
166// the indices are reversed.
167std::string ArrayIndexString(const std::vector<unsigned int> &indices);
Jamie Madill53cb14d2014-07-08 15:02:35 -0400168
169inline std::string Str(int i)
170{
171 std::stringstream strstr;
172 strstr << i;
173 return strstr.str();
174}
175
Austin Kinross922a9fb2014-10-21 14:26:33 -0700176size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
177
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400178std::string FormatString(const char *fmt, va_list vararg);
179std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400180
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400181template <typename T>
182std::string ToString(const T &value)
183{
184 std::ostringstream o;
185 o << value;
186 return o.str();
187}
188
Geoff Langdf8fafe2014-11-11 11:11:33 -0500189// snprintf is not defined with MSVC prior to to msvc14
190#if defined(_MSC_VER) && _MSC_VER < 1900
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000191#define snprintf _snprintf
192#endif
193
Geoff Langaadc8f32017-08-11 17:34:44 -0400194#define GL_BGRX8_ANGLEX 0x6ABA
Jamie Madillec0b5802016-07-04 13:11:59 -0400195#define GL_BGR565_ANGLEX 0x6ABB
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000196#define GL_BGRA4_ANGLEX 0x6ABC
197#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400198#define GL_INT_64_ANGLEX 0x6ABE
jchen10a99ed552017-09-22 08:10:32 +0800199#define GL_UINT_64_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000200
Geoff Lang7825f612014-11-26 16:19:41 -0500201// Hidden enum for the NULL D3D device type.
202#define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
203
Jamie Madill98de8262017-05-29 13:01:02 -0400204// TODO(jmadill): Clean this up at some point.
205#define EGL_PLATFORM_ANGLE_PLATFORM_METHODS_ANGLEX 0x9999
206
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500207#define ANGLE_TRY_CHECKED_MATH(result) \
208 if (!result.IsValid()) \
209 { \
210 return gl::InternalError() << "Integer overflow."; \
Jamie Madille2e406c2016-06-02 13:04:10 -0400211 }
212
Jamie Madill44183cc2017-08-01 12:48:34 -0400213// The below inlining code lifted from V8.
jchen103c76d592017-08-03 08:47:56 +0800214#if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
Jamie Madill44183cc2017-08-01 12:48:34 -0400215#define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE (__has_attribute(always_inline))
216#define ANGLE_HAS___FORCEINLINE 0
217#elif defined(_MSC_VER)
218#define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE 0
219#define ANGLE_HAS___FORCEINLINE 1
220#else
221#define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE 0
222#define ANGLE_HAS___FORCEINLINE 0
223#endif
224
225#if defined(NDEBUG) && ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE
226#define ANGLE_INLINE inline __attribute__((always_inline))
227#elif defined(NDEBUG) && ANGLE_HAS___FORCEINLINE
228#define ANGLE_INLINE __forceinline
229#else
230#define ANGLE_INLINE inline
231#endif
232
Jamie Madill92996b02017-08-17 10:39:02 -0400233#ifndef ANGLE_STRINGIFY
234#define ANGLE_STRINGIFY(x) #x
235#endif
236
237#ifndef ANGLE_MACRO_STRINGIFY
238#define ANGLE_MACRO_STRINGIFY(x) ANGLE_STRINGIFY(x)
239#endif
240
Jamie Madill71c88b32017-09-14 22:20:29 -0400241// Detect support for C++17 [[nodiscard]]
242#if !defined(__has_cpp_attribute)
243#define __has_cpp_attribute(name) 0
244#endif // !defined(__has_cpp_attribute)
245
246#if __has_cpp_attribute(nodiscard)
247#define ANGLE_NO_DISCARD [[nodiscard]]
248#else
249#define ANGLE_NO_DISCARD
250#endif // __has_cpp_attribute(nodiscard)
251
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000252#endif // COMMON_ANGLEUTILS_H_