1 /*
2 * Copyright (c) 2023 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
18 #include "EGL/egl.h"
19 #include "EGL/eglext.h"
20 #include "GLES3/gl32.h"
21
22 #include "draw/color.h"
23 #include "image/gpu_context.h"
24 #include "utils/log.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 constexpr int32_t EGL_CONTEXT_CLIENT_VERSION_NUM = 2;
33
34 class ShaderPersistentCache : public GPUContextOptions::PersistentCache {
35 public:
36 ShaderPersistentCache() = default;
37 ~ShaderPersistentCache() override = default;
38
Load(const Data & key)39 std::shared_ptr<Data> Load(const Data& key) override { return nullptr; };
Store(const Data & key,const Data & data)40 void Store(const Data& key, const Data& data) override {};
41 };
42
43 class GpuContextTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp() override;
48 void TearDown() override;
49
50 static void InitEGL();
51 static void DestroyEGL();
52
53 private:
54 static EGLDisplay eglDisplay_;
55 static EGLContext eglContext_;
56 };
57
58 EGLDisplay GpuContextTest::eglDisplay_ = EGL_NO_DISPLAY;
59 EGLContext GpuContextTest::eglContext_ = EGL_NO_CONTEXT;
60
SetUpTestCase()61 void GpuContextTest::SetUpTestCase()
62 {
63 InitEGL();
64 }
65
TearDownTestCase()66 void GpuContextTest::TearDownTestCase()
67 {
68 DestroyEGL();
69 }
70
SetUp()71 void GpuContextTest::SetUp() {}
TearDown()72 void GpuContextTest::TearDown() {}
73
InitEGL()74 void GpuContextTest::InitEGL()
75 {
76 LOGI("Creating EGLContext!!!");
77 eglDisplay_ = eglGetDisplay(static_cast<EGLNativeDisplayType>(EGL_DEFAULT_DISPLAY));
78 if (eglDisplay_ == EGL_NO_DISPLAY) {
79 LOGW("Failed to create EGLDisplay gl errno : %{public}x", eglGetError());
80 return;
81 }
82
83 EGLint major, minor;
84 if (eglInitialize(eglDisplay_, &major, &minor) == EGL_FALSE) {
85 LOGE("Failed to initialize EGLDisplay");
86 return;
87 }
88
89 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
90 LOGE("Failed to bind OpenGL ES API");
91 return;
92 }
93
94 unsigned int ret;
95 EGLConfig config;
96 EGLint count;
97 EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
98 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
99
100 ret = eglChooseConfig(eglDisplay_, configAttribs, &config, 1, &count);
101 if (!(ret && static_cast<unsigned int>(count) >= 1)) {
102 LOGE("Failed to eglChooseConfig");
103 return;
104 }
105
106 static const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE };
107
108 eglContext_ = eglCreateContext(eglDisplay_, config, EGL_NO_CONTEXT, contextAttribs);
109 if (eglContext_ == EGL_NO_CONTEXT) {
110 LOGE("Failed to create egl context %{public}x", eglGetError());
111 return;
112 }
113 if (!eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_)) {
114 LOGE("Failed to make current on surface, error is %{public}x", eglGetError());
115 return;
116 }
117
118 LOGI("Create EGL context successfully, version %{public}d.%{public}d", major, minor);
119 }
120
DestroyEGL()121 void GpuContextTest::DestroyEGL()
122 {
123 if (eglDisplay_ == EGL_NO_DISPLAY) {
124 return;
125 }
126
127 eglDestroyContext(eglDisplay_, eglContext_);
128 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
129 eglTerminate(eglDisplay_);
130 eglReleaseThread();
131
132 eglDisplay_ = EGL_NO_DISPLAY;
133 eglContext_ = EGL_NO_CONTEXT;
134 }
135
136 /**
137 * @tc.name: GPUContextCreateTest001
138 * @tc.desc: Test for creating GPUContext.
139 * @tc.type: FUNC
140 * @tc.require: I774GD
141 */
142 HWTEST_F(GpuContextTest, GPUContextCreateTest001, TestSize.Level1)
143 {
144 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
145 ASSERT_TRUE(gpuContext != nullptr);
146 }
147
148 /**
149 * @tc.name: GPUContextCreateTest001
150 * @tc.desc: Test for creating a GL GPUContext for a backend context.
151 * @tc.type: FUNC
152 * @tc.require: I774GD
153 */
154 HWTEST_F(GpuContextTest, BuildFromGLTest001, TestSize.Level1)
155 {
156 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
157 ASSERT_TRUE(gpuContext != nullptr);
158 GPUContextOptions options;
159 EXPECT_TRUE(gpuContext->BuildFromGL(options));
160
161 gpuContext->Flush();
162 std::chrono::milliseconds msNotUsed;
163 gpuContext->PerformDeferredCleanup(msNotUsed);
164 int32_t maxResource = 100;
165 size_t maxResourceBytes = 1000;
166 gpuContext->GetResourceCacheLimits(maxResource, maxResourceBytes);
167 gpuContext->SetResourceCacheLimits(maxResource, maxResourceBytes);
168 }
169
170 /**
171 * @tc.name: GPUContextCreateTest002
172 * @tc.desc: Test for creating a GL GPUContext for a backend context.
173 * @tc.type: FUNC
174 * @tc.require: I774GD
175 */
176 HWTEST_F(GpuContextTest, BuildFromGLTest002, TestSize.Level1)
177 {
178 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
179 ASSERT_TRUE(gpuContext != nullptr);
180 GPUContextOptions options;
181 auto persistentCache = std::make_shared<ShaderPersistentCache>();
182 options.SetPersistentCache(persistentCache.get());
183 EXPECT_TRUE(gpuContext->BuildFromGL(options));
184 }
185
186 /**
187 * @tc.name: FlushTest001
188 * @tc.desc: Test for flushing to underlying 3D API specific objects.
189 * @tc.type: FUNC
190 * @tc.require: I774GD
191 */
192 HWTEST_F(GpuContextTest, FlushTest001, TestSize.Level1)
193 {
194 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
195 ASSERT_TRUE(gpuContext != nullptr);
196 gpuContext->Flush();
197 }
198
199 /**
200 * @tc.name: PerformDeferredCleanupTest001
201 * @tc.desc: Test for Purging GPU resources that haven't been used in the past 'msNotUsed' milliseconds.
202 * @tc.type: FUNC
203 * @tc.require: I774GD
204 */
205 HWTEST_F(GpuContextTest, PerformDeferredCleanupTest001, TestSize.Level1)
206 {
207 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
208 ASSERT_TRUE(gpuContext != nullptr);
209 std::chrono::milliseconds msNotUsed;
210 gpuContext->PerformDeferredCleanup(msNotUsed);
211 }
212
213 /**
214 * @tc.name: GetResourceCacheLimitsTest001
215 * @tc.desc: Test for geting the current GPU resource cache limits.
216 * @tc.type: FUNC
217 * @tc.require: I774GD
218 */
219 HWTEST_F(GpuContextTest, GetResourceCacheLimitsTest001, TestSize.Level1)
220 {
221 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
222 ASSERT_TRUE(gpuContext != nullptr);
223 int32_t maxResource = 0;
224 size_t maxResourceBytes = 0;
225 gpuContext->GetResourceCacheLimits(maxResource, maxResourceBytes);
226 }
227
228 /**
229 * @tc.name: GetResourceCacheLimitsTest002
230 * @tc.desc: Test for geting the current GPU resource cache limits.
231 * @tc.type: FUNC
232 * @tc.require: I774GD
233 */
234 HWTEST_F(GpuContextTest, GetResourceCacheLimitsTest002, TestSize.Level1)
235 {
236 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
237 ASSERT_TRUE(gpuContext != nullptr);
238 int32_t maxResource = 10;
239 size_t maxResourceBytes = 1000;
240 gpuContext->GetResourceCacheLimits(maxResource, maxResourceBytes);
241 }
242
243 /**
244 * @tc.name: SetResourceCacheLimitsTest001
245 * @tc.desc: Test for set specify the GPU resource cache limits.
246 * @tc.type: FUNC
247 * @tc.require: I774GD
248 */
249 HWTEST_F(GpuContextTest, SetResourceCacheLimitsTest001, TestSize.Level1)
250 {
251 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
252 ASSERT_TRUE(gpuContext != nullptr);
253 int32_t maxResource = 0;
254 size_t maxResourceBytes = 0;
255 gpuContext->SetResourceCacheLimits(maxResource, maxResourceBytes);
256 }
257
258 /**
259 * @tc.name: SetResourceCacheLimitsTest002
260 * @tc.desc: Test for set specify the GPU resource cache limits.
261 * @tc.type: FUNC
262 * @tc.require: I774GD
263 */
264 HWTEST_F(GpuContextTest, SetResourceCacheLimitsTest002, TestSize.Level1)
265 {
266 std::unique_ptr<GPUContext> gpuContext = std::make_unique<GPUContext>();
267 ASSERT_TRUE(gpuContext != nullptr);
268 int32_t maxResource = 100;
269 size_t maxResourceBytes = 1000;
270 gpuContext->SetResourceCacheLimits(maxResource, maxResourceBytes);
271 }
272 } // namespace Drawing
273 } // namespace Rosen
274 } // namespace OHOS
275