blob: ea1a0cae36b1da6ebaff65258249cb72943fa392 [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
26class NonCopyable
27{
28 public:
29 NonCopyable() = default;
30 ~NonCopyable() = default;
31 protected:
32 NonCopyable(const NonCopyable&) = delete;
33 void operator=(const NonCopyable&) = delete;
34};
35
Jamie Madillc9bdeff2016-02-08 12:36:55 -050036extern const uintptr_t DirtyPointer;
Sami Väisänen46eaa942016-06-29 10:26:37 +030037} // namespace angle
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
Jamie Madill33ea2f92014-08-29 15:15:01 -040039template <typename T, size_t N>
Sami Väisänen46eaa942016-06-29 10:26:37 +030040constexpr inline size_t ArraySize(T (&)[N])
shannon.woods@transgaming.com6d792572013-02-28 23:07:25 +000041{
42 return N;
43}
44
shannon.woods%transgaming.com@gtempaccount.com96224772013-04-13 03:30:18 +000045template <typename T, unsigned int N>
46void SafeRelease(T (&resourceBlock)[N])
47{
48 for (unsigned int i = 0; i < N; i++)
49 {
50 SafeRelease(resourceBlock[i]);
51 }
52}
53
54template <typename T>
55void SafeRelease(T& resource)
56{
57 if (resource)
58 {
59 resource->Release();
60 resource = NULL;
61 }
62}
63
Geoff Langea228632013-07-30 15:17:12 -040064template <typename T>
Jamie Madill78a9c732016-07-15 11:22:43 -040065void SafeDelete(T *&resource)
Geoff Langea228632013-07-30 15:17:12 -040066{
67 delete resource;
68 resource = NULL;
69}
70
71template <typename T>
Geoff Lang04fb89a2014-06-09 15:05:36 -040072void SafeDeleteContainer(T& resource)
73{
Jamie Madill4e31ad52015-10-29 10:32:57 -040074 for (auto &element : resource)
Geoff Lang04fb89a2014-06-09 15:05:36 -040075 {
Jamie Madill4e31ad52015-10-29 10:32:57 -040076 SafeDelete(element);
Geoff Lang04fb89a2014-06-09 15:05:36 -040077 }
78 resource.clear();
79}
80
81template <typename T>
Geoff Langea228632013-07-30 15:17:12 -040082void SafeDeleteArray(T*& resource)
83{
84 delete[] resource;
85 resource = NULL;
86}
87
Jamie Madilld3f0f1e2013-09-20 13:31:08 -040088// Provide a less-than function for comparing structs
89// Note: struct memory must be initialized to zero, because of packing gaps
90template <typename T>
91inline bool StructLessThan(const T &a, const T &b)
92{
93 return (memcmp(&a, &b, sizeof(T)) < 0);
94}
95
96// Provide a less-than function for comparing structs
97// Note: struct memory must be initialized to zero, because of packing gaps
98template <typename T>
99inline bool StructEquals(const T &a, const T &b)
100{
101 return (memcmp(&a, &b, sizeof(T)) == 0);
102}
103
104template <typename T>
105inline void StructZero(T *obj)
106{
107 memset(obj, 0, sizeof(T));
108}
109
Tibor den Ouden2221f472014-10-22 15:07:05 +0200110template <typename T>
111inline bool IsMaskFlagSet(T mask, T flag)
112{
113 // Handles multibit flags as well
114 return (mask & flag) == flag;
115}
116
Geoff Langcec35902014-04-16 10:52:36 -0400117inline const char* MakeStaticString(const std::string &str)
118{
119 static std::set<std::string> strings;
120 std::set<std::string>::iterator it = strings.find(str);
121 if (it != strings.end())
122 {
123 return it->c_str();
124 }
125
126 return strings.insert(str).first->c_str();
127}
128
Jamie Madill53cb14d2014-07-08 15:02:35 -0400129inline std::string ArrayString(unsigned int i)
130{
131 // We assume UINT_MAX and GL_INVALID_INDEX are equal
132 // See DynamicHLSL.cpp
133 if (i == UINT_MAX)
134 {
135 return "";
136 }
137
138 std::stringstream strstr;
139
140 strstr << "[";
141 strstr << i;
142 strstr << "]";
143
144 return strstr.str();
145}
146
147inline std::string Str(int i)
148{
149 std::stringstream strstr;
150 strstr << i;
151 return strstr.str();
152}
153
Austin Kinross922a9fb2014-10-21 14:26:33 -0700154size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& buffer);
155
Shannon Woods8e7d7a32014-09-02 17:09:08 -0400156std::string FormatString(const char *fmt, va_list vararg);
157std::string FormatString(const char *fmt, ...);
Geoff Langda5777c2014-07-11 09:52:58 -0400158
Geoff Langdf8fafe2014-11-11 11:11:33 -0500159// snprintf is not defined with MSVC prior to to msvc14
160#if defined(_MSC_VER) && _MSC_VER < 1900
alokp@chromium.org79fb1012012-04-26 21:07:39 +0000161#define snprintf _snprintf
162#endif
163
Jamie Madillec0b5802016-07-04 13:11:59 -0400164#define GL_BGR565_ANGLEX 0x6ABB
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000165#define GL_BGRA4_ANGLEX 0x6ABC
166#define GL_BGR5_A1_ANGLEX 0x6ABD
Jamie Madill0fda9862013-07-19 16:36:55 -0400167#define GL_INT_64_ANGLEX 0x6ABE
Jamie Madill28167c62013-08-30 13:21:10 -0400168#define GL_STRUCT_ANGLEX 0x6ABF
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000169
Geoff Lang7825f612014-11-26 16:19:41 -0500170// Hidden enum for the NULL D3D device type.
171#define EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE 0x6AC0
172
Jamie Madille2e406c2016-06-02 13:04:10 -0400173#define ANGLE_TRY_CHECKED_MATH(result) \
174 if (!result.IsValid()) \
175 { \
176 return gl::Error(GL_INVALID_OPERATION, "Integer overflow."); \
177 }
178
daniel@transgaming.combbf56f72010-04-20 18:52:13 +0000179#endif // COMMON_ANGLEUTILS_H_