blob: 1cbb9686c7c6b3d545430c206df2e2c50236b0dc [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;
Sami Väisänen46eaa942016-06-29 10:26:37 +030042} // namespace angle
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
Jamie Madill33ea2f92014-08-29 15:15:01 -040044template <typename T, size_t N>
Sami Väisänen46eaa942016-06-29 10:26:37 +030045constexpr inline size_t ArraySize(T (&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000046{
47 return N;
48}
49
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000050template <typename T, unsigned int N>
51void SafeRelease(T (&resourceBlock)[N])
52{
53 for (unsigned int i = 0; i < N; i++)
54 {
55 SafeRelease(resourceBlock[i]);
56 }
57}
58
59template <typename T>
60void SafeRelease(T& resource)
61{
62 if (resource)
63 {
64 resource->Release();
Yunchao Hed7297bf2017-04-19 15:27:10 +080065 resource = nullptr;
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000066 }
67}
68
Geoff Langea228632013-07-30 15:17:12 -040069template <typename T>
Jamie Madill78a9c732016-07-15 11:22:43 -040070void SafeDelete(T *&resource)
Geoff Langea228632013-07-30 15:17:12 -040071{
72 delete resource;
Yunchao Hed7297bf2017-04-19 15:27:10 +080073 resource = nullptr;
Geoff Langea228632013-07-30 15:17:12 -040074}
75
76template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040077void SafeDeleteContainer(T& resource)
78{
Jamie Madill4e31ad52015-10-29 10:32:57 -040079 for (auto &element : resource)
Geoff Lang04fb89a2014-06-09 15:05:36 -040080 {
Jamie Madill4e31ad52015-10-29 10:32:57 -040081 SafeDelete(element);
Geoff Lang04fb89a2014-06-09 15:05:36 -040082 }
83 resource.clear();
84}
85
86template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040087void SafeDeleteArray(T*& resource)
88{
89 delete[] resource;
Yunchao Hed7297bf2017-04-19 15:27:10 +080090 resource = nullptr;
Geoff Langea228632013-07-30 15:17:12 -040091}
92
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040093// Provide a less-than function for comparing structs
94// Note: struct memory must be initialized to zero, because of packing gaps
95template <typename T>
96inline bool StructLessThan(const T &a, const T &b)
97{
98 return (memcmp(&a, &b, sizeof(T)) < 0);
99}
100
101// Provide a less-than function for comparing structs
102// Note: struct memory must be initialized to zero, because of packing gaps
103template <typename T>
104inline bool StructEquals(const T &a, const T &b)
105{
106 return (memcmp(&a, &b, sizeof(T)) == 0);
107}
108
109template <typename T>
110inline void StructZero(T *obj)
111{
112 memset(obj, 0, sizeof(T));
113}
114
Tibor den Ouden2221f472014-10-22 15:07:05 +0200115template <typename T>
116inline bool IsMaskFlagSet(T mask, T flag)
117{
118 // Handles multibit flags as well
119 return (mask & flag) == flag;
120}
121
Geoff Langcec35902014-04-16 10:52:36 -0400122inline const char* MakeStaticString(const std::string &str)
123{
124 static std::set<std::string> strings;
125 std::set<std::string>::iterator it = strings.find(str);
126 if (it != strings.end())
127 {
128 return it->c_str();
129 }
130
131 return strings.insert(str).first->c_str();
132}
133
Jamie Madill53cb14d2014-07-08 15:02:35 -0400134inline std::string ArrayString(unsigned int i)
135{
136 // We assume UINT_MAX and GL_INVALID_INDEX are equal
137 // See DynamicHLSL.cpp
138 if (i == UINT_MAX)
139 {
140 return "";
141 }
142
143 std::stringstream strstr;
144
145 strstr << "[";
146 strstr << i;
147 strstr << "]";
148
149 return strstr.str();
150}
151
152inline std::string Str(int i)
153{
154 std::stringstream strstr;
155 strstr << i;
156 return strstr.str();
157}
158
Austin Kinross922a9fb2014-10-21 14:26:33 -0700159size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
160
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400161std::string FormatString(const char *fmt, va_list vararg);
162std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400163
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400164template <typename T>
165std::string ToString(const T &value)
166{
167 std::ostringstream o;
168 o << value;
169 return o.str();
170}
171
Geoff Langdf8fafe2014-11-11 11:11:33 -0500172// snprintf is not defined with MSVC prior to to msvc14
173#if defined(_MSC_VER) && _MSC_VER < 1900
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000174#define snprintf _snprintf
175#endif
176
Jamie Madillec0b5802016-07-04 13:11:59 -0400177#define GL_BGR565_ANGLEX 0x6ABB
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000178#define GL_BGRA4_ANGLEX 0x6ABC
179#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400180#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400181#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000182
Geoff Lang7825f612014-11-26 16:19:41 -0500183// Hidden enum for the NULL D3D device type.
184#define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
185
Jamie Madille2e406c2016-06-02 13:04:10 -0400186#define ANGLE_TRY_CHECKED_MATH(result) \
187 if (!result.IsValid()) \
188 { \
189 return gl::Error(GL_INVALID_OPERATION, "Integer overflow."); \
190 }
191
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000192#endif // COMMON_ANGLEUTILS_H_