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 "window.h"
21 #include "core/ui/rs_surface_node.h"
22
23 #include "drawing_canvas.h"
24 #include "drawing_error_code.h"
25 #include "drawing_gpu_context.h"
26 #include "drawing_surface.h"
27 #include "drawing_surface_utils.h"
28
29 #ifdef RS_ENABLE_VK
30 #include "platform/ohos/backend/rs_vulkan_context.h"
31 #endif
32
33 using namespace testing;
34 using namespace testing::ext;
35
36 namespace OHOS {
37 namespace Rosen {
38 namespace Drawing {
39
40 constexpr static int32_t WIDTH = 720;
41 constexpr static int32_t HEIGHT = 1280;
42
43 class NativeDrawingSurfaceTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp() override;
48 void TearDown() override;
49 protected:
50 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
51 EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
52 EGLContext eglContext_ = EGL_NO_CONTEXT;
53 EGLSurface eglSurface_ = EGL_NO_SURFACE;
54 OH_Drawing_GpuContext* gpuContext_ = nullptr;
55 OH_Drawing_Surface* surface_ = nullptr;
56 OH_Drawing_Canvas* canvas_ = nullptr;
57 NativeWindow *window_ = nullptr;
58 std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
59 };
60
SetUpTestCase()61 void NativeDrawingSurfaceTest::SetUpTestCase()
62 {
63 #ifdef RS_ENABLE_VK
64 RsVulkanContext::SetRecyclable(false);
65 #endif
66 }
TearDownTestCase()67 void NativeDrawingSurfaceTest::TearDownTestCase() {}
SetUp()68 void NativeDrawingSurfaceTest::SetUp()
69 {
70 eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
71 EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
72
73 EGLint eglMajVers;
74 EGLint eglMinVers;
75 EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
76 EXPECT_EQ(ret, EGL_TRUE);
77
78 EGLint count;
79 EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
80 EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
81 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
82 ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
83 EXPECT_EQ(ret, EGL_TRUE);
84 EXPECT_GE(count, 1);
85
86 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
87 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
88 EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
89
90 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
91 eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
92 EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
93
94 ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
95 EXPECT_EQ(ret, EGL_TRUE);
96
97 RSSurfaceNodeConfig config;
98 surfaceNode = RSSurfaceNode::Create(config);
99 EXPECT_NE(surfaceNode, nullptr);
100 sptr<OHOS::Surface> surf = surfaceNode->GetSurface();
101 window_ = CreateNativeWindowFromSurface(&surf);
102 EXPECT_NE(window_, nullptr);
103
104 NativeWindowHandleOpt(window_, SET_USAGE, BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA);
105 NativeWindowHandleOpt(window_, SET_BUFFER_GEOMETRY, WIDTH, HEIGHT);
106 NativeWindowHandleOpt(window_, SET_COLOR_GAMUT, GRAPHIC_COLOR_GAMUT_SRGB);
107 }
108
TearDown()109 void NativeDrawingSurfaceTest::TearDown()
110 {
111 EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
112 EXPECT_EQ(ret, EGL_TRUE);
113
114 ret = eglDestroyContext(eglDisplay_, eglContext_);
115 EXPECT_EQ(ret, EGL_TRUE);
116
117 ret = eglTerminate(eglDisplay_);
118 EXPECT_EQ(ret, EGL_TRUE);
119
120 eglSurface_ = EGL_NO_SURFACE;
121 eglContext_ = EGL_NO_CONTEXT;
122 eglDisplay_ = EGL_NO_DISPLAY;
123 }
124
125 /*
126 * @tc.name: NativeDrawingSurfaceTest_CreateFromGpuContext
127 * @tc.desc: test for CreateFromGpuContext.
128 * @tc.type: FUNC
129 * @tc.require: AR000GTO5R
130 */
131 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_CreateFromGpuContext, TestSize.Level1)
132 {
133 OH_Drawing_GpuContextOptions options {true};
134 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
135 EXPECT_NE(gpuContext_, nullptr);
136
137 const int32_t width = 500;
138 const int32_t height = 500;
139 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
140 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
141 EXPECT_NE(surface_, nullptr);
142 OH_Drawing_SurfaceDestroy(surface_);
143
144 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo);
145 EXPECT_NE(surface_, nullptr);
146 OH_Drawing_SurfaceDestroy(surface_);
147
148 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(nullptr, false, imageInfo);
149 EXPECT_EQ(surface_, nullptr);
150 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
151 OH_Drawing_SurfaceDestroy(surface_);
152 OH_Drawing_GpuContextDestroy(gpuContext_);
153 }
154
155 /*
156 * @tc.name: NativeDrawingSurfaceTest_CreateOnScreen
157 * @tc.desc: test for CreateOnScreen.
158 * @tc.type: FUNC
159 * @tc.require: AR000GTO5R
160 */
161 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_CreateOnScreen, TestSize.Level1)
162 {
163 gpuContext_ = OH_Drawing_GpuContextCreate();
164 EXPECT_NE(gpuContext_, nullptr);
165
166 const int32_t width = 500;
167 const int32_t height = 500;
168 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
169 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, window_);
170 EXPECT_NE(surface_, nullptr);
171 OH_Drawing_SurfaceDestroy(surface_);
172
173 surface_ = OH_Drawing_SurfaceCreateOnScreen(nullptr, imageInfo, window_);
174 EXPECT_EQ(surface_, nullptr);
175 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
176 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, nullptr);
177 EXPECT_EQ(surface_, nullptr);
178 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
179 surface_ = OH_Drawing_SurfaceCreateOnScreen(nullptr, imageInfo, nullptr);
180 EXPECT_EQ(surface_, nullptr);
181 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
182
183 OH_Drawing_SurfaceDestroy(surface_);
184 OH_Drawing_GpuContextDestroy(gpuContext_);
185 }
186
187 /*
188 * @tc.name: NativeDrawingSurfaceTest_GetCanvas
189 * @tc.desc: test for GetCanvas.
190 * @tc.type: FUNC
191 * @tc.require: AR000GTO5R
192 */
193 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_GetCanvas, TestSize.Level1)
194 {
195 OH_Drawing_GpuContextOptions options {true};
196 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
197 EXPECT_NE(gpuContext_, nullptr);
198
199 const int32_t width = 500;
200 const int32_t height = 500;
201 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
202 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
203 EXPECT_NE(surface_, nullptr);
204
205 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_);
206 EXPECT_NE(canvas_, nullptr);
207
208 canvas_ = OH_Drawing_SurfaceGetCanvas(nullptr);
209 EXPECT_EQ(canvas_, nullptr);
210 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
211 OH_Drawing_SurfaceDestroy(surface_);
212 OH_Drawing_GpuContextDestroy(gpuContext_);
213 }
214
215 /*
216 * @tc.name: NativeDrawingSurfaceTest_Flush
217 * @tc.desc: test for Flush.
218 * @tc.type: FUNC
219 * @tc.require: AR000GTO5R
220 */
221 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_Flush, TestSize.Level1)
222 {
223 gpuContext_ = OH_Drawing_GpuContextCreate();
224 EXPECT_NE(gpuContext_, nullptr);
225
226 const int32_t width = 500;
227 const int32_t height = 500;
228 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
229 surface_ = OH_Drawing_SurfaceCreateOnScreen(gpuContext_, imageInfo, window_);
230 EXPECT_NE(surface_, nullptr);
231 OH_Drawing_SurfaceDestroy(surface_);
232
233 OH_Drawing_ErrorCode errorCode = OH_Drawing_SurfaceFlush(nullptr);
234 EXPECT_EQ(errorCode, OH_DRAWING_ERROR_INVALID_PARAMETER);
235
236 OH_Drawing_GpuContextDestroy(gpuContext_);
237 }
238
239 /*
240 * @tc.name: NativeDrawingSurfaceTest_Utils
241 * @tc.desc: test for Utils.
242 * @tc.type: FUNC
243 * @tc.require: AR000GTO5R
244 */
245 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_Utils, TestSize.Level1)
246 {
247 const int32_t width = 500;
248 const int32_t height = 500;
249 ImageInfo imageInfo(width, height, COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE);
250 std::shared_ptr<Drawing::Surface> surface = DrawingSurfaceUtils::CreateFromWindow(nullptr, imageInfo, window_);
251 EXPECT_EQ(surface_, nullptr);
252 surface = DrawingSurfaceUtils::CreateFromWindow(nullptr, imageInfo, nullptr);
253 EXPECT_EQ(surface_, nullptr);
254 bool ret = DrawingSurfaceUtils::FlushSurface(nullptr);
255 EXPECT_EQ(ret, false);
256 }
257 } // namespace Drawing
258 } // namespace Rosen
259 } // namespace OHOS