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