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, Hardware
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 #include "gtest/gtest.h"
17 #include "EGL/egl.h"
18 #include "EGL/eglext.h"
19 #include "GLES3/gl32.h"
20 #include "drawing_gpu_context.h"
21 #include "drawing_gpu_context_manager.h"
22
23 #ifdef RS_ENABLE_VK
24 #include "platform/ohos/backend/rs_vulkan_context.h"
25 #endif
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 namespace Drawing {
33 class NativeDrawingGpuContextTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39 protected:
40 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
41 EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
42 EGLContext eglContext_ = EGL_NO_CONTEXT;
43 EGLSurface eglSurface_ = EGL_NO_SURFACE;
44 OH_Drawing_GpuContext* gpuContext_ = nullptr;
45 };
46
SetUpTestCase()47 void NativeDrawingGpuContextTest::SetUpTestCase()
48 {
49 #ifdef RS_ENABLE_VK
50 RsVulkanContext::SetRecyclable(false);
51 #endif
52 }
TearDownTestCase()53 void NativeDrawingGpuContextTest::TearDownTestCase() {}
SetUp()54 void NativeDrawingGpuContextTest::SetUp()
55 {
56 eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
57 EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
58
59 EGLint eglMajVers;
60 EGLint eglMinVers;
61 EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
62 EXPECT_EQ(ret, EGL_TRUE);
63
64 EGLint count;
65 EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
66 EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
67 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
68 ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
69 EXPECT_EQ(ret, EGL_TRUE);
70 EXPECT_GE(count, 1);
71
72 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
73 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
74 EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
75
76 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
77 eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
78 EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
79
80 ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
81 EXPECT_EQ(ret, EGL_TRUE);
82 }
83
TearDown()84 void NativeDrawingGpuContextTest::TearDown()
85 {
86 EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
87 EXPECT_EQ(ret, EGL_TRUE);
88
89 ret = eglDestroyContext(eglDisplay_, eglContext_);
90 EXPECT_EQ(ret, EGL_TRUE);
91
92 ret = eglTerminate(eglDisplay_);
93 EXPECT_EQ(ret, EGL_TRUE);
94
95 eglSurface_ = EGL_NO_SURFACE;
96 eglContext_ = EGL_NO_CONTEXT;
97 eglDisplay_ = EGL_NO_DISPLAY;
98 }
99
100 /*
101 * @tc.name: NativeDrawingGpuContextTest_CreateFromGL
102 * @tc.desc: test for CreateFromGL.
103 * @tc.type: FUNC
104 * @tc.require: AR000GTO5R
105 */
106 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_CreateFromGL, TestSize.Level1)
107 {
108 OH_Drawing_GpuContextOptions options;
109 options.allowPathMaskCaching = true;
110 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
111 EXPECT_NE(gpuContext_, nullptr);
112 OH_Drawing_GpuContextDestroy(gpuContext_);
113
114 options.allowPathMaskCaching = false;
115 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
116 EXPECT_NE(gpuContext_, nullptr);
117 OH_Drawing_GpuContextDestroy(gpuContext_);
118 }
119
120 /*
121 * @tc.name: NativeDrawingGpuContextTest_Create
122 * @tc.desc: test for Create.
123 * @tc.type: FUNC
124 * @tc.require: AR000GTO5R
125 */
126 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_Create, TestSize.Level1)
127 {
128 gpuContext_ = OH_Drawing_GpuContextCreate();
129 EXPECT_NE(gpuContext_, nullptr);
130 OH_Drawing_GpuContextDestroy(gpuContext_);
131 }
132
133 /*
134 * @tc.name: NativeDrawingGpuContextTest_Manager
135 * @tc.desc: test for Manager.
136 * @tc.type: FUNC
137 * @tc.require: AR000GTO5R
138 */
139 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_Manager, TestSize.Level1)
140 {
141 std::shared_ptr<GPUContext> context = DrawingGpuContextManager::GetInstance().Find(nullptr);
142 EXPECT_EQ(context, nullptr);
143 bool ret = DrawingGpuContextManager::GetInstance().Remove(nullptr);
144 EXPECT_EQ(ret, false);
145 }
146
147 /*
148 * @tc.name: NativeDrawingGpuContextTest_Manager2
149 * @tc.desc: test for Manager.
150 * @tc.type: FUNC
151 * @tc.require: AR000GTO5R
152 */
153 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_Manager2, TestSize.Level1)
154 {
155 GPUContext* context = new GPUContext();
156 DrawingGpuContextManager::GetInstance().Insert(context, nullptr);
157 bool ret = DrawingGpuContextManager::GetInstance().Remove(context);
158 EXPECT_EQ(ret, true);
159 delete context;
160 }
161
162 /*
163 * @tc.name: NativeDrawingGpuContextTest_GpuContextDestroy
164 * @tc.desc: test for GpuContextDestroy.
165 * @tc.type: FUNC
166 * @tc.require: AR000GTO5R
167 */
168 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_GpuContextDestroy, TestSize.Level1)
169 {
170 OH_Drawing_GpuContext *gpuContext_ = nullptr;
171 OH_Drawing_GpuContextDestroy(gpuContext_);
172 EXPECT_EQ(gpuContext_, nullptr);
173 }
174 } // namespace Drawing
175 } // namespace Rosen
176 } // namespace OHOS