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 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class NativeDrawingGpuContextTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 protected:
35     EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
36     EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
37     EGLContext eglContext_ = EGL_NO_CONTEXT;
38     EGLSurface eglSurface_ = EGL_NO_SURFACE;
39     OH_Drawing_GpuContext* gpuContext_ = nullptr;
40 };
41 
SetUpTestCase()42 void NativeDrawingGpuContextTest::SetUpTestCase() {}
TearDownTestCase()43 void NativeDrawingGpuContextTest::TearDownTestCase() {}
SetUp()44 void NativeDrawingGpuContextTest::SetUp()
45 {
46     eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
47     EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
48 
49     EGLint eglMajVers;
50     EGLint eglMinVers;
51     EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
52     EXPECT_EQ(ret, EGL_TRUE);
53 
54     EGLint count;
55     EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
56         EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
57         EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
58     ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
59     EXPECT_EQ(ret, EGL_TRUE);
60     EXPECT_GE(count, 1);
61 
62     const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
63     eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
64     EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
65 
66     EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
67     eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
68     EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
69 
70     ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
71     EXPECT_EQ(ret, EGL_TRUE);
72 }
73 
TearDown()74 void NativeDrawingGpuContextTest::TearDown()
75 {
76     EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
77     EXPECT_EQ(ret, EGL_TRUE);
78 
79     ret = eglDestroyContext(eglDisplay_, eglContext_);
80     EXPECT_EQ(ret, EGL_TRUE);
81 
82     ret = eglTerminate(eglDisplay_);
83     EXPECT_EQ(ret, EGL_TRUE);
84 
85     eglSurface_ = EGL_NO_SURFACE;
86     eglContext_ = EGL_NO_CONTEXT;
87     eglDisplay_ = EGL_NO_DISPLAY;
88 }
89 
90 /*
91  * @tc.name: NativeDrawingGpuContextTest_CreateFromGL
92  * @tc.desc: test for CreateFromGL.
93  * @tc.type: FUNC
94  * @tc.require: AR000GTO5R
95  */
96 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_CreateFromGL, TestSize.Level1)
97 {
98     OH_Drawing_GpuContextOptions options;
99     options.allowPathMaskCaching = true;
100     gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
101     EXPECT_NE(gpuContext_, nullptr);
102     OH_Drawing_GpuContextDestroy(gpuContext_);
103 
104     options.allowPathMaskCaching = false;
105     gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
106     EXPECT_NE(gpuContext_, nullptr);
107     OH_Drawing_GpuContextDestroy(gpuContext_);
108 }
109 
110 /*
111  * @tc.name: NativeDrawingGpuContextTest_GpuContextDestroy
112  * @tc.desc: test for GpuContextDestroy.
113  * @tc.type: FUNC
114  * @tc.require: AR000GTO5R
115  */
116 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_GpuContextDestroy, TestSize.Level1)
117 {
118     OH_Drawing_GpuContext *gpuContext_ = nullptr;
119     OH_Drawing_GpuContextDestroy(gpuContext_);
120     EXPECT_EQ(gpuContext_, nullptr);
121 }
122 } // namespace Drawing
123 } // namespace Rosen
124 } // namespace OHOS