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 OHOS_ACELITE_ACE_DFX_ADAPTER_H
17 #define OHOS_ACELITE_ACE_DFX_ADAPTER_H
18 
19 #include <cstdint>
20 #include "jsi_types.h"
21 
22 namespace OHOS {
23 namespace ACELite {
24 /**
25  * This handler type is so specific for product, should be designed from OS dfx level.
26  */
27 typedef void (*EventPrintHandler)(uint8_t info1, uint8_t info2, uint8_t info3, uint8_t info4);
28 typedef void (*ErrCodePrintHandler)(uint8_t info1, uint8_t info2, uint8_t info3, uint16_t info4);
29 
30 /**
31  * The hook for outputing user's console log.
32  */
33 typedef void (*JSLogOutputHandler)(uint8_t level, const char *content);
34 
35 /**
36  * The wrapper data structure for get native memory pool info.
37  */
38 struct NativeMemInfo {
39     uint32_t totalSize; /* mem pool total size */
40     uint32_t freeSize;  /* total free size */
41 };
42 
43 /**
44  * Native memory pool info getter.
45  */
46 typedef void (*NativeMemInfoGetter)(NativeMemInfo *memInfo);
47 
48 /**
49  * Product modules hook.
50  */
51 typedef Module *(*ProductModulesGetter)(uint16_t &moduleCount);
52 
53 /**
54  * Private modules hook.
55  */
56 typedef PrivateModule *(*PrivateModulesGetter)(uint16_t &moduleCount);
57 
58 /**
59  * The hook for terminating ability.
60  */
61 typedef void (*TerminateAbilityHandler)(uint32_t token, bool forceStop);
62 
63 /**
64  * The hook for restore fail dump message.
65  */
66 typedef void (*RestoreSystemHandler)(const char *crashMessage);
67 
68 /**
69  * The hook for determining the capability of supporting PNG and JPG image formats.
70  */
71 typedef bool (*IsPNGSupportedHandler)(const char *imagePath, const char *bundleName);
72 
73 /**
74  * The hook for set component views parameters.
75  */
76 typedef void (*SetViewsParaHandler)(void *ComponentHandler);
77 
78 /**
79  * As all the UI event handling is driven by the render tick, and to make sure the the event handling is
80  * in JS task, the HAL layer will transfer the TE event into the loop of JS task, when JS application go foreground.
81  */
82 typedef void (*RenderTEHandler)();
83 typedef void (*VoidFuncHook)();
84 
85 /**
86  * The hook for screen on visible.
87  */
88 typedef bool (*SetScreenOnVisibleHandler)(bool visible);
89 
90 /**
91  * The hook for update default font.
92  */
93 typedef void (*UpdateDefaultFontHandler)(const char* curLanguage, const char* curOrigion, bool hasJson);
94 
95 /**
96  * The wrapper data structure for get native memory pool info.
97  */
98 struct TEHandlingHooks {
99     RenderTEHandler renderTEHandler;
100     VoidFuncHook renderEndHandler;
101 };
102 
103 enum TEDispatchingResult : uint8_t {
104     REFUSED = 0, // not on foreground, will refuse TE, vsync should send it to other
105     ACCEPTED, // on foreground, and TE event is received successfully
106     ACCEPT_FAILED, // on foreground, but TE event can not be received, vsync should be retried to send to ACE again
107 };
108 
109 /**
110  * The wrapper data structure for get native memory pool info.
111  */
112 struct ExtraPresetModulesHook {
113     VoidFuncHook loadingHandler;
114     VoidFuncHook unloadingHandler;
115 };
116 
117 /**
118  * The wrapper class for some product feature implementation, since some interfaces are provided by specific product.
119  */
120 class ProductAdapter final {
121 public:
122     ProductAdapter(const ProductAdapter &) = delete;
123     ProductAdapter &operator=(const ProductAdapter &) = delete;
124     ProductAdapter(ProductAdapter &&) = delete;
125     ProductAdapter &operator=(ProductAdapter &&) = delete;
126     ProductAdapter() = delete;
127     ~ProductAdapter() = delete;
128 
129     // initialization functions
130     static void InitTraceHandlers(EventPrintHandler eventHandler, ErrCodePrintHandler errCodeHandler);
131     static void InitAceTags(uint8_t *aceTags, uint8_t tagCount);
132     static void InitConsoleNativeHandler(JSLogOutputHandler handler);
133     static void InitNativeMemPoolHook(NativeMemInfoGetter getter);
134     static void InitExtraModulesGetter(ProductModulesGetter productModuleGetter,
135                                        PrivateModulesGetter privateModuleGetter);
136     static void InitDeviceInfo(const char *deviceType);
137     static void RegTerminatingHandler(TerminateAbilityHandler handler);
138     static void RegTEHandlers(const TEHandlingHooks &teHandlingHooks);
139     static TEDispatchingResult DispatchTEMessage();
140     static void SetDefaultFontStyle(const char *defaultFontFamily, uint8_t defaultFontSize);
141     static void SetScreenSize(uint16_t width, uint16_t height);
142     static void RegSetScreenOnVisibleHandler(SetScreenOnVisibleHandler handler);
143     static void RegExtraPresetModulesHook(ExtraPresetModulesHook hook);
144     static void ConfigPrivateDataRootPath(const char *appDataRoot);
145     static void RegRestoreSystemHandler(RestoreSystemHandler handler);
146     static void RegIsPNGSupportedHandler(IsPNGSupportedHandler handler);
147     static void RegSetViewsParaHandler(SetViewsParaHandler handler);
148     static void RegUpdateDefaultFontHandler(UpdateDefaultFontHandler handler);
149 
150     // wrapper functions, for ace internal calling
151     static void PrintEventTrace(uint8_t info2, uint8_t info3, uint8_t info4);
152     static void PrintErrCode(uint8_t info2, uint16_t rfu);
153     static void OutputJSConsoleLog(uint8_t level, const char *content);
154     static void GetNativeMemInfo(NativeMemInfo *memInfo);
155     static void SendTerminatingRequest(uint32_t token, bool forceStop);
156     static bool UpdateDefaultFont();
157     static bool IsTEHandlersRegisted();
158     static void ProcessOneTE();
159     static void NotifyRenderEnd();
160     static const char *GetDefaultFontFamilyName();
161     static uint8_t GetDefaultFontSize();
162     static void UpdateRenderTickAcceptable(bool acceptable);
163     static void GetScreenSize(uint16_t &width, uint16_t &height);
164     static const char *GetDeviceType();
165     static bool SetScreenOnVisible(bool visible);
166     static void LoadExtraPresetModules();
167     static void UnloadExtraPresetModules();
168     static const char *GetPrivateDataRootPath();
169     static void RestoreSystemWrapper(const char *crashMessage);
170     static bool IsPNGSupportedWrapper(const char *imagePath, const char *bundleName);
171     static void SetViewsParaWrapper(void *ComponentHandler);
172 };
173 } // namespace ACELite
174 } // namespace OHOS
175 #endif // OHOS_ACELITE_ACE_DFX_ADAPTER_H
176