1 /*
2 * Copyright (c) 2021-2023 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 #include <unistd.h>
26
27 namespace OHOS {
28 #endif
29
30 #include "graphic_common_c.h"
31
32 #ifdef __cplusplus
33 namespace {
34 const std::map<GSError, std::string> GSErrorStrs = {
35 {GSERROR_OK, "<200 ok>"},
36 {GSERROR_INVALID_ARGUMENTS, "<400 invalid arguments>"},
37 {GSERROR_NO_PERMISSION, "<403 no permission>"},
38 {GSERROR_CONNOT_CONNECT_SAMGR, "<404 connot connect to samgr>"},
39 {GSERROR_CONNOT_CONNECT_SERVER, "<404 connot connect to server>"},
40 {GSERROR_CONNOT_CONNECT_WESTON, "<404 connot connect to weston>"},
41 {GSERROR_NO_BUFFER, "<406 no buffer>"},
42 {GSERROR_NO_ENTRY, "<406 no entry>"},
43 {GSERROR_OUT_OF_RANGE, "<406 out of range>"},
44 {GSERROR_NO_SCREEN, "<406 no screen>"},
45 {GSERROR_INVALID_OPERATING, "<412 invalid operating>"},
46 {GSERROR_NO_CONSUMER, "<412 no consumer>"},
47 {GSERROR_NOT_INIT, "<412 not init>"},
48 {GSERROR_TYPE_ERROR, "<412 type error>"},
49 {GSERROR_API_FAILED, "<500 api call failed>"},
50 {GSERROR_INTERNAL, "<500 internal error>"},
51 {GSERROR_NO_MEM, "<500 no memory>"},
52 {GSERROR_PROXY_NOT_INCLUDE, "<500 proxy not include>"},
53 {GSERROR_SERVER_ERROR, "<500 server occur error>"},
54 {GSERROR_ANIMATION_RUNNING, "<500 animation is running>"},
55 {GSERROR_NOT_IMPLEMENT, "<501 not implement>"},
56 {GSERROR_NOT_SUPPORT, "<501 not support>"},
57 {GSERROR_BINDER, "<504 binder occur error>"},
58 };
59 }
60
LowErrorStrSpecial(GSError err)61 inline std::string LowErrorStrSpecial(GSError err)
62 {
63 if (err == LOWERROR_INVALID) {
64 // int to string (in 1000)
65 char num[] = {
66 static_cast<char>(err / 0x64 % 0xa), static_cast<char>(err / 0xa % 0xa), static_cast<char>(err % 0xa), 0
67 };
68 return std::string("with low error <") + num + ">";
69 } else if (err == LOWERROR_FAILURE) {
70 return "with low error <failure>";
71 }
72 return "";
73 }
74
75 #ifdef _WIN32
76 #define strerror_r(err, buf, len) strerror_s((buf), (len), (err))
77 #endif
78
LowErrorStr(GSError lowerr)79 inline std::string LowErrorStr(GSError lowerr)
80 {
81 std::string lowError = LowErrorStrSpecial(lowerr);
82 if (lowError == "" && lowerr != 0) {
83 char buf[256] = {0}; // 256 mean buffer max length
84 strerror_r(lowerr, buf, sizeof buf);
85 lowError = std::string("with low error <") + buf + ">";
86 }
87 return lowError;
88 }
89
GSErrorStr(GSError err)90 inline std::string GSErrorStr(GSError err)
91 {
92 GSError diff = static_cast<GSError>(err % LOWERROR_MAX);
93 auto it = GSErrorStrs.find(static_cast<GSError>(err - diff));
94 if (it == GSErrorStrs.end()) {
95 return "<GSError error index out of range>";
96 }
97 return it->second + LowErrorStr(diff);
98 }
99
WMErrorStr(GSError err)100 inline std::string WMErrorStr(GSError err)
101 {
102 return GSErrorStr(err);
103 }
104
SurfaceErrorStr(GSError err)105 inline std::string SurfaceErrorStr(GSError err)
106 {
107 return GSErrorStr(err);
108 }
109
VsyncErrorStr(GSError err)110 inline std::string VsyncErrorStr(GSError err)
111 {
112 return GSErrorStr(err);
113 }
114
115 inline std::ostream &operator <<(std::ostream &os, const GSError &err)
116 {
117 os << GSErrorStr(err);
118 return os;
119 }
120
121 inline bool operator ==(GSError a, GSError b)
122 {
123 return static_cast<int32_t>(a) / LOWERROR_MAX == static_cast<int32_t>(b) / LOWERROR_MAX;
124 }
125
126 inline bool operator !=(GSError a, GSError b)
127 {
128 return static_cast<int32_t>(a) / LOWERROR_MAX != static_cast<int32_t>(b) / LOWERROR_MAX;
129 }
130
131 using WMError = GSError;
132 using SurfaceError = GSError;
133 using VsyncError = GSError;
134 } // namespace OHOS
135 #endif // __cplusplus
136
137 #endif // INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
138