1 /*
2  * Copyright (c) 2024 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 MY_XXOMPONENT_H
17 #define MY_XXOMPONENT_H
18 #include <ace/xcomponent/native_interface_xcomponent.h>
19 #include <arpa/nameser.h>
20 #include <bits/alltypes.h>
21 #include <cstdint>
22 #include <EGL/egl.h>
23 #include <EGL/eglext.h>
24 #include <GLES3/gl3.h>
25 #include <map>
26 #include <native_drawing/drawing_bitmap.h>
27 #include <native_drawing/drawing_color.h>
28 #include <native_drawing/drawing_canvas.h>
29 #include <native_drawing/drawing_gpu_context.h>
30 #include <native_drawing/drawing_pen.h>
31 #include <native_drawing/drawing_brush.h>
32 #include <native_drawing/drawing_path.h>
33 #include <native_drawing/drawing_rect.h>
34 #include <native_drawing/drawing_surface.h>
35 #include <native_window/external_window.h>
36 #include <string>
37 #include <sys/mman.h>
38 #include "napi/native_api.h"
39 #include "test_base.h"
40 
41 class MyXComponent {
42 public:
43     MyXComponent() = default;
44     ~MyXComponent();
MyXComponent(std::string id)45     explicit MyXComponent(std::string id) : id_(id) {}
46     static MyXComponent *GetOrCreateInstance(std::string &id);
47     static void Release(std::string &id);
48 
49     void Export(napi_env env, napi_value exports);
50     static napi_value NapiSetTestCount(napi_env env, napi_callback_info info);
51     static napi_value NapiGetTime(napi_env env, napi_callback_info info);
52     static napi_value NapiGetTestNames(napi_env env, napi_callback_info info);
53     static napi_value NapiFunctionCpu(napi_env env, napi_callback_info info);
54     static napi_value NapiFunctionGpu(napi_env env, napi_callback_info info);
55     static napi_value NapiPerformanceCpu(napi_env env, napi_callback_info info);
56     static napi_value NapiPerformanceGpu(napi_env env, napi_callback_info info);
57     static napi_value NapiStabilityCpu(napi_env env, napi_callback_info info);
58 
59     // display to screen
60     void RegisterCallback(OH_NativeXComponent *nativeXComponent);
61     void SetNativeWindow(OHNativeWindow *nativeWindow);
62     void SetScreenWidth(uint64_t width);
63     void SetSceenHeight(uint64_t height);
64 
65     // for test
66     void SetTestCount(uint32_t testCount);
67     uint32_t GetTime();
68     std::string GetTestNames();
69     void TestFunctionCpu(napi_env env, std::string caseName);
70     void TestFunctionGpu(napi_env env, std::string caseName);
71     void TestPerformanceCpu(napi_env env, std::string caseName);
72     void TestPerformanceGpu(napi_env env, std::string caseName);
73     void TestStabilityCpu(napi_env env, std::string caseName);
74     void TestStabilityCpuInner(std::string caseName);
75     std::string id_;
76 
77 private:
78     void InitScreenCanvas();
79     void BitmapToScreenCanvas(OH_Drawing_Bitmap* bitmap);
80     void FlushScreen();
81     void DeInitializeEglContext();
82     int32_t InitializeEglContext();
83 
84     OHNativeWindow *nativeWindow_ = nullptr;
85     EGLDisplay mEGLDisplay = EGL_NO_DISPLAY;
86     EGLConfig mEGLConfig = nullptr;
87     EGLContext mEGLContext = EGL_NO_CONTEXT;
88     EGLSurface mEGLSurface = nullptr;
89     // This is the size of the display area, not the size of the entire screen
90     uint64_t screenWidth_ = 0;
91     uint64_t screenHeight_ = 0;
92     OH_Drawing_Bitmap *screenBitmap_ = nullptr;
93     OH_Drawing_Canvas *screenCanvas_ = nullptr;
94     OH_Drawing_Image_Info screenImageInfo_;
95     OH_NativeXComponent_Callback renderCallback_;
96     uint32_t *mappedAddr_ = nullptr;
97     BufferHandle *bufferHandle_ = nullptr;
98     struct NativeWindowBuffer *buffer_ = nullptr;
99     int fenceFd_ = 0;
100     uint32_t testCount_ = DEFAULT_TESTCOUNT;
101     uint32_t usedTime_ = 0;
102 };
103 #endif
104