• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NO_BUFFER_READY,       "<406 no buffer ready>"},
46     {GSERROR_INVALID_OPERATING,     "<412 invalid operating>"},
47     {GSERROR_NO_CONSUMER,           "<412 no consumer>"},
48     {GSERROR_NOT_INIT,              "<412 not init>"},
49     {GSERROR_TYPE_ERROR,            "<412 type error>"},
50     {GSERROR_CONSUMER_IS_CONNECTED, "<412 consumer is connected>"},
51     {GSERROR_BUFFER_STATE_INVALID,  "<412 buffer state invalid>"},
52     {GSERROR_BUFFER_IS_INCACHE,     "<412 buffer is incache>"},
53     {GSERROR_BUFFER_QUEUE_FULL,     "<412 buffer queue full>"},
54     {GSERROR_BUFFER_NOT_INCACHE,    "<412 buffer not in cache>"},
55     {GSERROR_CONSUMER_DISCONNECTED, "<412 consumer disconnected>"},
56     {GSERROR_CONSUMER_UNREGISTER_LISTENER, "<412 consumer unregister listener>"},
57     {GSERROR_API_FAILED,            "<500 api call failed>"},
58     {GSERROR_INTERNAL,              "<500 internal error>"},
59     {GSERROR_NO_MEM,                "<500 no memory>"},
60     {GSERROR_PROXY_NOT_INCLUDE,     "<500 proxy not include>"},
61     {GSERROR_SERVER_ERROR,          "<500 server occur error>"},
62     {GSERROR_ANIMATION_RUNNING,     "<500 animation is running>"},
63     {GSERROR_HDI_ERROR,             "<500 hdi error>"},
64     {GSERROR_NOT_IMPLEMENT,         "<501 not implement>"},
65     {GSERROR_NOT_SUPPORT,           "<501 not support>"},
66     {GSERROR_BINDER,                "<504 binder occur error>"},
67     {GSERROR_EGL_STATE_UNKOWN,      "<600 egl state unknown>"},
68     {GSERROR_EGL_API_FAILED,        "<600 egl api falied>"},
69 };
70 }
71 
LowErrorStrSpecial(GSError err)72 inline std::string LowErrorStrSpecial(GSError err)
73 {
74     if (err == LOWERROR_INVALID) {
75         // int to string (in 1000)
76         char num[] = {
77             static_cast<char>(err / 0x64 % 0xa), static_cast<char>(err / 0xa % 0xa), static_cast<char>(err % 0xa), 0
78         };
79         return std::string("with low error <") + num + ">";
80     } else if (err == LOWERROR_FAILURE) {
81         return "with low error <failure>";
82     }
83     return "";
84 }
85 
86 #ifdef _WIN32
87 #define strerror_r(err, buf, len) strerror_s((buf), (len), (err))
88 #endif
89 
LowErrorStr(GSError lowerr)90 inline std::string LowErrorStr(GSError lowerr)
91 {
92     std::string lowError = LowErrorStrSpecial(lowerr);
93     if (lowError == "" && lowerr != 0) {
94         char buf[256] = {0}; // 256 mean buffer max length
95         strerror_r(lowerr, buf, sizeof buf);
96         lowError = std::string("with low error <") + buf + ">";
97     }
98     return lowError;
99 }
100 
GSErrorStr(GSError err)101 inline std::string GSErrorStr(GSError err)
102 {
103     GSError diff = static_cast<GSError>(err % LOWERROR_MAX);
104     auto it = GSErrorStrs.find(static_cast<GSError>(err - diff));
105     if (it == GSErrorStrs.end()) {
106         return "<GSError error index out of range>";
107     }
108     return it->second + LowErrorStr(diff);
109 }
110 
SurfaceErrorStr(GSError err)111 inline std::string SurfaceErrorStr(GSError err)
112 {
113     return GSErrorStr(err);
114 }
115 
116 inline std::ostream &operator <<(std::ostream &os, const GSError &err)
117 {
118     os << GSErrorStr(err);
119     return os;
120 }
121 
122 inline bool operator ==(GSError a, GSError b)
123 {
124     return static_cast<int32_t>(a) / LOWERROR_MAX == static_cast<int32_t>(b) / LOWERROR_MAX;
125 }
126 
127 inline bool operator !=(GSError a, GSError b)
128 {
129     return static_cast<int32_t>(a) / LOWERROR_MAX != static_cast<int32_t>(b) / LOWERROR_MAX;
130 }
131 
132 using WMError = GSError;
133 using SurfaceError = GSError;
134 using VsyncError = GSError;
135 } // namespace OHOS
136 #endif // __cplusplus
137 
138 #endif // INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
139