blob: 49c071ce8b8e11726b2b97d689ce6a3fe443b71b [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
Jamie Madill47c0e042016-11-30 13:44:45 -050026#if defined(ANBLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
27using Microsoft::WRL::ComPtr;
28#endif // defined(ANBLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
29
Jamie Madillf0d10f82015-03-31 12:56:52 -040030class NonCopyable
31{
32 public:
33 NonCopyable() = default;
34 ~NonCopyable() = default;
35 protected:
36 NonCopyable(const NonCopyable&) = delete;
37 void operator=(const NonCopyable&) = delete;
38};
39
Jamie Madillc9bdeff2016-02-08 12:36:55 -050040extern const uintptr_t DirtyPointer;
Sami Väisänen46eaa942016-06-29 10:26:37 +030041} // namespace angle
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042
Jamie Madill33ea2f92014-08-29 15:15:01 -040043template <typename T, size_t N>
Sami Väisänen46eaa942016-06-29 10:26:37 +030044constexpr inline size_t ArraySize(T (&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000045{
46 return N;
47}
48
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000049template <typename T, unsigned int N>
50void SafeRelease(T (&resourceBlock)[N])
51{
52 for (unsigned int i = 0; i < N; i++)
53 {
54 SafeRelease(resourceBlock[i]);
55 }
56}
57
58template <typename T>
59void SafeRelease(T& resource)
60{
61 if (resource)
62 {
63 resource->Release();
64 resource = NULL;
65 }
66}
67
Geoff Langea228632013-07-30 15:17:12 -040068template <typename T>
Jamie Madill78a9c732016-07-15 11:22:43 -040069void SafeDelete(T *&resource)
Geoff Langea228632013-07-30 15:17:12 -040070{
71 delete resource;
72 resource = NULL;
73}
74
75template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040076void SafeDeleteContainer(T& resource)
77{
Jamie Madill4e31ad52015-10-29 10:32:57 -040078 for (auto &element : resource)
Geoff Lang04fb89a2014-06-09 15:05:36 -040079 {
Jamie Madill4e31ad52015-10-29 10:32:57 -040080 SafeDelete(element);
Geoff Lang04fb89a2014-06-09 15:05:36 -040081 }
82 resource.clear();
83}
84
85template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040086void SafeDeleteArray(T*& resource)
87{
88 delete[] resource;
89 resource = NULL;
90}
91
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040092// Provide a less-than function for comparing structs
93// Note: struct memory must be initialized to zero, because of packing gaps
94template <typename T>
95inline 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
102template <typename T>
103inline bool StructEquals(const T &a, const T &b)
104{
105 return (memcmp(&a, &b, sizeof(T)) == 0);
106}
107
108template <typename T>
109inline void StructZero(T *obj)
110{
111 memset(obj, 0, sizeof(T));
112}
113
Tibor den Ouden2221f472014-10-22 15:07:05 +0200114template <typename T>
115inline bool IsMaskFlagSet(T mask, T flag)
116{
117 // Handles multibit flags as well
118 return (mask & flag) == flag;
119}
120
Geoff Langcec35902014-04-16 10:52:36 -0400121inline 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
Jamie Madill53cb14d2014-07-08 15:02:35 -0400133inline std::string ArrayString(unsigned int i)
134{
135 // We assume UINT_MAX and GL_INVALID_INDEX are equal
136 // See DynamicHLSL.cpp
137 if (i == UINT_MAX)
138 {
139 return "";
140 }
141
142 std::stringstream strstr;
143
144 strstr << "[";
145 strstr << i;
146 strstr << "]";
147
148 return strstr.str();
149}
150
151inline std::string Str(int i)
152{
153 std::stringstream strstr;
154 strstr << i;
155 return strstr.str();
156}
157
Austin Kinross922a9fb2014-10-21 14:26:33 -0700158size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
159
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400160std::string FormatString(const char *fmt, va_list vararg);
161std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400162
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400163template <typename T>
164std::string ToString(const T &value)
165{
166 std::ostringstream o;
167 o << value;
168 return o.str();
169}
170
Geoff Langdf8fafe2014-11-11 11:11:33 -0500171// snprintf is not defined with MSVC prior to to msvc14
172#if defined(_MSC_VER) && _MSC_VER < 1900
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000173#define snprintf _snprintf
174#endif
175
Jamie Madillec0b5802016-07-04 13:11:59 -0400176#define GL_BGR565_ANGLEX 0x6ABB
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000177#define GL_BGRA4_ANGLEX 0x6ABC
178#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400179#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400180#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000181
Geoff Lang7825f612014-11-26 16:19:41 -0500182// Hidden enum for the NULL D3D device type.
183#define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
184
Jamie Madille2e406c2016-06-02 13:04:10 -0400185#define ANGLE_TRY_CHECKED_MATH(result) \
186 if (!result.IsValid()) \
187 { \
188 return gl::Error(GL_INVALID_OPERATION, "Integer overflow."); \
189 }
190
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000191#endif // COMMON_ANGLEUTILS_H_