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
22using namespace testing;
23using namespace testing::ext;
24
25namespace OHOS {
26namespace Rosen {
27namespace Drawing {
28class 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
43void DrawingNativeGpuContextTest::SetUpTestCase() {}
44void DrawingNativeGpuContextTest::TearDownTestCase() {}
45void 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
87void 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 */
111HWTEST_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 */
127HWTEST_F(DrawingNativeGpuContextTest, testGpuContextDestroyNULL, TestSize.Level3) {
128    OH_Drawing_GpuContextDestroy(nullptr);
129}
130
131/*
132 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_GPU_CONTEXT_0102
133 * @tc.name: testGpuContextCreateFromGLDestroyMultipleCalls
134 * @tc.desc: Test for repeatedly creating and destroying GPU context.
135 * @tc.size  : SmallTest
136 * @tc.type  : Function
137 * @tc.level : Level 3
138 */
139HWTEST_F(DrawingNativeGpuContextTest, testGpuContextCreateFromGLDestroyMultipleCalls, TestSize.Level3) {
140    for (int i = 0; i < 10; i++) {
141        OH_Drawing_GpuContextOptions options;
142        options.allowPathMaskCaching = true;
143        gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
144        EXPECT_NE(gpuContext_, nullptr);
145        OH_Drawing_GpuContextDestroy(gpuContext_);
146    }
147    for (int i = 0; i < 10; i++) {
148        OH_Drawing_GpuContextOptions options;
149        options.allowPathMaskCaching = true;
150        gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
151        EXPECT_NE(gpuContext_, nullptr);
152    }
153    OH_Drawing_GpuContextOptions options;
154    options.allowPathMaskCaching = true;
155    gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
156    for (int i = 0; i < 10; i++) {
157        OH_Drawing_GpuContextDestroy(gpuContext_);
158    }
159}
160
161} // namespace Drawing
162} // namespace Rosen
163} // namespace OHOS