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_canvas.h"
21 #include "drawing_gpu_context.h"
22 #include "drawing_surface.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class NativeDrawingSurfaceTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 protected:
37 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
38 EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
39 EGLContext eglContext_ = EGL_NO_CONTEXT;
40 EGLSurface eglSurface_ = EGL_NO_SURFACE;
41 OH_Drawing_GpuContext* gpuContext_ = nullptr;
42 OH_Drawing_Surface* surface_ = nullptr;
43 OH_Drawing_Canvas* canvas_ = nullptr;
44 };
45
SetUpTestCase()46 void NativeDrawingSurfaceTest::SetUpTestCase() {}
TearDownTestCase()47 void NativeDrawingSurfaceTest::TearDownTestCase() {}
SetUp()48 void NativeDrawingSurfaceTest::SetUp()
49 {
50 eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
51 EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
52
53 EGLint eglMajVers;
54 EGLint eglMinVers;
55 EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
56 EXPECT_EQ(ret, EGL_TRUE);
57
58 EGLint count;
59 EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
60 EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
61 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
62 ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
63 EXPECT_EQ(ret, EGL_TRUE);
64 EXPECT_GE(count, 1);
65
66 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
67 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
68 EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
69
70 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
71 eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
72 EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
73
74 ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
75 EXPECT_EQ(ret, EGL_TRUE);
76 }
77
TearDown()78 void NativeDrawingSurfaceTest::TearDown()
79 {
80 EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
81 EXPECT_EQ(ret, EGL_TRUE);
82
83 ret = eglDestroyContext(eglDisplay_, eglContext_);
84 EXPECT_EQ(ret, EGL_TRUE);
85
86 ret = eglTerminate(eglDisplay_);
87 EXPECT_EQ(ret, EGL_TRUE);
88
89 eglSurface_ = EGL_NO_SURFACE;
90 eglContext_ = EGL_NO_CONTEXT;
91 eglDisplay_ = EGL_NO_DISPLAY;
92 }
93
94 /*
95 * @tc.name : NativeDrawingSurfaceTest_CreateFromGpuContext
96 * @tc.desc : test for CreateFromGpuContext.
97 * @tc.size : MediumTest
98 * @tc.type : Function
99 * @tc.level : Level 1
100 */
101 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_CreateFromGpuContext, TestSize.Level1)
102 {
103 OH_Drawing_GpuContextOptions options {true};
104 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options);
105 EXPECT_NE(gpuContext_, nullptr);
106
107 const int32_t width = 500;
108 const int32_t height = 500;
109 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
110 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo);
111 EXPECT_NE(surface_, nullptr);
112 OH_Drawing_SurfaceDestroy(surface_);
113
114 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo);
115 EXPECT_NE(surface_, nullptr);
116 OH_Drawing_SurfaceDestroy(surface_);
117
118 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(nullptr, false, imageInfo);
119 EXPECT_EQ(surface_, nullptr);
120 OH_Drawing_SurfaceDestroy(surface_);
121 OH_Drawing_GpuContextDestroy(gpuContext_);
122 }
123
124 /*
125 * @tc.name : NativeDrawingSurfaceTest_GetCanvas
126 * @tc.desc : test for GetCanvas.
127 * @tc.size : MediumTest
128 * @tc.type : Function
129 * @tc.level : Level 1
130 */
131 HWTEST_F(NativeDrawingSurfaceTest, NativeDrawingSurfaceTest_GetCanvas, 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
143 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_);
144 EXPECT_NE(canvas_, nullptr);
145
146 canvas_ = OH_Drawing_SurfaceGetCanvas(nullptr);
147 EXPECT_EQ(canvas_, nullptr);
148 OH_Drawing_SurfaceDestroy(surface_);
149 OH_Drawing_GpuContextDestroy(gpuContext_);
150 }
151 } // namespace Drawing
152 } // namespace Rosen
153 } // namespace OHOS