• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 #include "EGL/egl.h"
17 #include "EGL/eglext.h"
18 #include "drawing_bitmap.h"
19 #include "drawing_gpu_context.h"
20 #include "gtest/gtest.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class DrawingNativeGpuContextTest : public testing::Test {
29   public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 
35   protected:
36     EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
37     EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
38     EGLContext eglContext_ = EGL_NO_CONTEXT;
39     EGLSurface eglSurface_ = EGL_NO_SURFACE;
40     OH_Drawing_GpuContext *gpuContext_ = nullptr;
41 };
42 
SetUpTestCase()43 void DrawingNativeGpuContextTest::SetUpTestCase() {}
TearDownTestCase()44 void DrawingNativeGpuContextTest::TearDownTestCase() {}
SetUp()45 void DrawingNativeGpuContextTest::SetUp()
46 {
47     eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
48     EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
49 
50     EGLint eglMajVers;
51     EGLint eglMinVers;
52     EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
53     EXPECT_EQ(ret, EGL_TRUE);
54 
55     EGLint count;
56     EGLint configAttribs[] = {
57         EGL_SURFACE_TYPE,
58         EGL_WINDOW_BIT,
59         EGL_RED_SIZE,
60         8,
61         EGL_GREEN_SIZE,
62         8,
63         EGL_BLUE_SIZE,
64         8,
65         EGL_ALPHA_SIZE,
66         8,
67         EGL_RENDERABLE_TYPE,
68         EGL_OPENGL_ES3_BIT,
69         EGL_NONE,
70     };
71     ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
72     EXPECT_EQ(ret, EGL_TRUE);
73     EXPECT_GE(count, 1);
74 
75     const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
76     eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
77     EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
78 
79     EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
80     eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
81     EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
82 
83     ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
84     EXPECT_EQ(ret, EGL_TRUE);
85 }
86 
TearDown()87 void DrawingNativeGpuContextTest::TearDown()
88 {
89     EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
90     EXPECT_EQ(ret, EGL_TRUE);
91 
92     ret = eglDestroyContext(eglDisplay_, eglContext_);
93     EXPECT_EQ(ret, EGL_TRUE);
94 
95     ret = eglTerminate(eglDisplay_);
96     EXPECT_EQ(ret, EGL_TRUE);
97 
98     eglSurface_ = EGL_NO_SURFACE;
99     eglContext_ = EGL_NO_CONTEXT;
100     eglDisplay_ = EGL_NO_DISPLAY;
101 }
102 
103 /*
104  * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_GPU_CONTEXT_0100
105  * @tc.name: testGpuContextCreateFromGLDestroyNormal
106  * @tc.desc: Test for creating and destroying GPU context with normal parameters.
107  * @tc.size  : SmallTest
108  * @tc.type  : Function
109  * @tc.level : Level 0
110  */
111 HWTEST_F(DrawingNativeGpuContextTest, testGpuContextCreateFromGLDestroyNormal, TestSize.Level0) {
112     OH_Drawing_GpuContextOptions options;
113     options.allowPathMaskCaching = true;
114     gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
115     EXPECT_NE(gpuContext_, nullptr);
116     OH_Drawing_GpuContextDestroy(gpuContext_);
117 }
118 
119 /*
120  * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_GPU_CONTEXT_0101
121  * @tc.name: testGpuContextDestroyNULL
122  * @tc.desc: Test for destroying GPU context with NULL parameter.
123  * @tc.size  : SmallTest
124  * @tc.type  : Function
125  * @tc.level : Level 3
126  */
127 HWTEST_F(DrawingNativeGpuContextTest, testGpuContextDestroyNULL, TestSize.Level3) {
128     OH_Drawing_GpuContextDestroy(nullptr);
129     // add assert
130     EXPECT_TRUE(true);
131 }
132 
133 /*
134  * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_GPU_CONTEXT_0102
135  * @tc.name: testGpuContextCreateFromGLDestroyMultipleCalls
136  * @tc.desc: Test for repeatedly creating and destroying GPU context.
137  * @tc.size  : SmallTest
138  * @tc.type  : Function
139  * @tc.level : Level 3
140  */
141 HWTEST_F(DrawingNativeGpuContextTest, testGpuContextCreateFromGLDestroyMultipleCalls, TestSize.Level3) {
142     for (int i = 0; i < 10; i++) {
143         OH_Drawing_GpuContextOptions options;
144         options.allowPathMaskCaching = true;
145         gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
146         EXPECT_NE(gpuContext_, nullptr);
147         OH_Drawing_GpuContextDestroy(gpuContext_);
148     }
149 }
150 
151 /*
152  * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_GPU_CONTEXT_0200
153  * @tc.name: testGpuContextCreateNormal
154  * @tc.desc: Test for creating GPU context with normal parameters.
155  * @tc.size  : SmallTest
156  * @tc.type  : Function
157  * @tc.level : Level 0
158  */
159 HWTEST_F(DrawingNativeGpuContextTest, testGpuContextCreateNormal, TestSize.Level0) {
160 
161     gpuContext_ = OH_Drawing_GpuContextCreate();
162     EXPECT_NE(gpuContext_, nullptr);
163     OH_Drawing_GpuContextDestroy(gpuContext_);
164 }
165 
166 } // namespace Drawing
167 } // namespace Rosen
168 } // namespace OHOS