1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
17 #define INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
18
19 #ifdef __cplusplus
20 #include <cstdint>
21 #include <cstring>
22 #include <map>
23 #include <string>
24 #include <ostream>
25
26 namespace OHOS {
27 #endif
28
29 #include "graphic_common_c.h"
30
31 #ifdef __cplusplus
32 static const std::map<GSError, std::string> GSErrorStrs = {
33 {GSERROR_OK, "<200 ok>"},
34 {GSERROR_INVALID_ARGUMENTS, "<400 invalid arguments>"},
35 {GSERROR_NO_PERMISSION, "<403 no permission>"},
36 {GSERROR_CONNOT_CONNECT_SAMGR, "<404 connot connect to samgr>"},
37 {GSERROR_CONNOT_CONNECT_SERVER, "<404 connot connect to server>"},
38 {GSERROR_CONNOT_CONNECT_WESTON, "<404 connot connect to weston>"},
39 {GSERROR_NO_BUFFER, "<406 no buffer>"},
40 {GSERROR_NO_ENTRY, "<406 no entry>"},
41 {GSERROR_OUT_OF_RANGE, "<406 out of range>"},
42 {GSERROR_NO_SCREEN, "<406 no screen>"},
43 {GSERROR_INVALID_OPERATING, "<412 invalid operating>"},
44 {GSERROR_NO_CONSUMER, "<412 no consumer>"},
45 {GSERROR_NOT_INIT, "<412 not init>"},
46 {GSERROR_TYPE_ERROR, "<412 type error>"},
47 {GSERROR_API_FAILED, "<500 api call failed>"},
48 {GSERROR_INTERNEL, "<500 internal error>"},
49 {GSERROR_NO_MEM, "<500 no memory>"},
50 {GSERROR_PROXY_NOT_INCLUDE, "<500 proxy not include>"},
51 {GSERROR_SERVER_ERROR, "<500 server occur error>"},
52 {GSERROR_ANIMATION_RUNNING, "<500 animation is running>"},
53 {GSERROR_NOT_IMPLEMENT, "<501 not implement>"},
54 {GSERROR_NOT_SUPPORT, "<501 not support>"},
55 {GSERROR_BINDER, "<504 binder occur error>"},
56 };
57
LowErrorStrSpecial(GSError err)58 static inline std::string LowErrorStrSpecial(GSError err)
59 {
60 if (err == LOWERROR_INVALID) {
61 return "with low error <invalid>";
62 } else if (err == LOWERROR_FAILURE) {
63 return "with low error <failure>";
64 }
65 return "";
66 }
67
LowErrorStr(GSError err)68 static inline std::string LowErrorStr(GSError err)
69 {
70 std::string lowError = LowErrorStrSpecial(err);
71 if (lowError == "" && err != 0) {
72 char str[] = {static_cast<char>(((err / 0x64) % 0xa) + '0'), static_cast<char>(((err / 0xa) % 0xa) + '0'),
73 static_cast<char>((err % 0xa) + '0'), '\0'}; // int to string (in 1000)
74 lowError = std::string("with low error <") + str + ">";
75 }
76 return lowError;
77 }
78
GSErrorStr(GSError err)79 static inline std::string GSErrorStr(GSError err)
80 {
81 GSError diff = static_cast<GSError>(err % LOWERROR_MAX);
82 auto it = GSErrorStrs.find(static_cast<GSError>(err - diff));
83 if (it == GSErrorStrs.end()) {
84 return "<GSError error index out of range>";
85 }
86 return it->second + LowErrorStr(diff);
87 }
88
WMErrorStr(GSError err)89 static inline std::string WMErrorStr(GSError err)
90 {
91 return GSErrorStr(err);
92 }
93
SurfaceErrorStr(GSError err)94 static inline std::string SurfaceErrorStr(GSError err)
95 {
96 return GSErrorStr(err);
97 }
98
VsyncErrorStr(GSError err)99 static inline std::string VsyncErrorStr(GSError err)
100 {
101 return GSErrorStr(err);
102 }
103
104 static inline std::ostream &operator <<(std::ostream &os, const GSError &err)
105 {
106 os << GSErrorStr(err);
107 return os;
108 }
109
110 static inline bool operator ==(GSError a, GSError b)
111 {
112 return static_cast<int32_t>(a) / LOWERROR_MAX == static_cast<int32_t>(b) / LOWERROR_MAX;
113 }
114
115 static inline bool operator !=(GSError a, GSError b)
116 {
117 return static_cast<int32_t>(a) / LOWERROR_MAX != static_cast<int32_t>(b) / LOWERROR_MAX;
118 }
119
120 using WMError = GSError;
121 using SurfaceError = GSError;
122 using VsyncError = GSError;
123 } // namespace OHOS
124 #endif // __cplusplus
125
126 #endif // INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
127