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 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 class NativeDrawingGpuContextTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
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 NativeDrawingGpuContextTest::SetUpTestCase() {}
TearDownTestCase()44 void NativeDrawingGpuContextTest::TearDownTestCase() {}
SetUp()45 void NativeDrawingGpuContextTest::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[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
57 EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
58 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
59 ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
60 EXPECT_EQ(ret, EGL_TRUE);
61 EXPECT_GE(count, 1);
62
63 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
64 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
65 EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
66
67 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
68 eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
69 EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
70
71 ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
72 EXPECT_EQ(ret, EGL_TRUE);
73 }
74
TearDown()75 void NativeDrawingGpuContextTest::TearDown()
76 {
77 EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
78 EXPECT_EQ(ret, EGL_TRUE);
79
80 ret = eglDestroyContext(eglDisplay_, eglContext_);
81 EXPECT_EQ(ret, EGL_TRUE);
82
83 ret = eglTerminate(eglDisplay_);
84 EXPECT_EQ(ret, EGL_TRUE);
85
86 eglSurface_ = EGL_NO_SURFACE;
87 eglContext_ = EGL_NO_CONTEXT;
88 eglDisplay_ = EGL_NO_DISPLAY;
89 }
90
91 /*
92 * @tc.name: NativeDrawingGpuContextTest_CreateFromGL
93 * @tc.desc: test for CreateFromGL.
94 * @tc.type: FUNC
95 * @tc.require: AR000GTO5R
96 */
97 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_CreateFromGL, TestSize.Level1)
98 {
99 OH_Drawing_GpuContextOptions options;
100 options.allowPathMaskCaching = true;
101 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
102 EXPECT_NE(gpuContext_, nullptr);
103 OH_Drawing_GpuContextDestroy(gpuContext_);
104
105 options.allowPathMaskCaching = false;
106 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
107 EXPECT_NE(gpuContext_, nullptr);
108 OH_Drawing_GpuContextDestroy(gpuContext_);
109 }
110
111 /*
112 * @tc.name: NativeDrawingGpuContextTest_Create
113 * @tc.desc: test for Create.
114 * @tc.type: FUNC
115 * @tc.require: AR000GTO5R
116 */
117 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_Create, TestSize.Level1)
118 {
119 gpuContext_ = OH_Drawing_GpuContextCreate();
120 EXPECT_NE(gpuContext_, nullptr);
121 OH_Drawing_GpuContextDestroy(gpuContext_);
122 }
123
124 /*
125 * @tc.name: NativeDrawingGpuContextTest_Manager
126 * @tc.desc: test for Manager.
127 * @tc.type: FUNC
128 * @tc.require: AR000GTO5R
129 */
130 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_Manager, TestSize.Level1)
131 {
132 std::shared_ptr<GPUContext> context = DrawingGpuContextManager::GetInstance().Find(nullptr);
133 EXPECT_EQ(context, nullptr);
134 bool ret = DrawingGpuContextManager::GetInstance().Remove(nullptr);
135 EXPECT_EQ(ret, false);
136 }
137
138 /*
139 * @tc.name: NativeDrawingGpuContextTest_GpuContextDestroy
140 * @tc.desc: test for GpuContextDestroy.
141 * @tc.type: FUNC
142 * @tc.require: AR000GTO5R
143 */
144 HWTEST_F(NativeDrawingGpuContextTest, NativeDrawingGpuContextTest_GpuContextDestroy, TestSize.Level1)
145 {
146 OH_Drawing_GpuContext *gpuContext_ = nullptr;
147 OH_Drawing_GpuContextDestroy(gpuContext_);
148 EXPECT_EQ(gpuContext_, nullptr);
149 }
150 } // namespace Drawing
151 } // namespace Rosen
152 } // namespace OHOS