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_error_code.h"
22 #include "drawing_gpu_context.h"
23 #include "drawing_surface.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace Drawing {
31 class NativeDrawingSurfaceTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp() override;
36     void TearDown() override;
37 protected:
38     EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
39     EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
40     EGLContext eglContext_ = EGL_NO_CONTEXT;
41     EGLSurface eglSurface_ = EGL_NO_SURFACE;
42     OH_Drawing_GpuContext* gpuContext_ = nullptr;
43     OH_Drawing_Surface* surface_ = nullptr;
44     OH_Drawing_Canvas* canvas_ = nullptr;
45 };
46 
SetUpTestCase()47 void NativeDrawingSurfaceTest::SetUpTestCase() {}
TearDownTestCase()48 void NativeDrawingSurfaceTest::TearDownTestCase() {}
SetUp()49 void NativeDrawingSurfaceTest::SetUp()
50 {
51     eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
52     EXPECT_NE(eglDisplay_, EGL_NO_DISPLAY);
53 
54     EGLint eglMajVers;
55     EGLint eglMinVers;
56     EGLBoolean ret = eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
57     EXPECT_EQ(ret, EGL_TRUE);
58 
59     EGLint count;
60     EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8,
61         EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
62         EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
63     ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count);
64     EXPECT_EQ(ret, EGL_TRUE);
65     EXPECT_GE(count, 1);
66 
67     const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
68     eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs);
69     EXPECT_NE(eglContext_, EGL_NO_CONTEXT);
70 
71     EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
72     eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs);
73     EXPECT_NE(eglSurface_, EGL_NO_SURFACE);
74 
75     ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_);
76     EXPECT_EQ(ret, EGL_TRUE);
77 }
78 
TearDown()79 void NativeDrawingSurfaceTest::TearDown()
80 {
81     EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_);
82     EXPECT_EQ(ret, EGL_TRUE);
83 
84     ret = eglDestroyContext(eglDisplay_, eglContext_);
85     EXPECT_EQ(ret, EGL_TRUE);
86 
87     ret = eglTerminate(eglDisplay_);
88     EXPECT_EQ(ret, EGL_TRUE);
89 
90     eglSurface_ = EGL_NO_SURFACE;
91     eglContext_ = EGL_NO_CONTEXT;
92     eglDisplay_ = EGL_NO_DISPLAY;
93 }
94 
95 /*
96  * @tc.name: NativeDrawingSurfaceTest_CreateFromGpuContext
97  * @tc.desc: test for CreateFromGpuContext.
98  * @tc.type: FUNC
99  * @tc.require: AR000GTO5R
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     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
121     OH_Drawing_SurfaceDestroy(surface_);
122     OH_Drawing_GpuContextDestroy(gpuContext_);
123 }
124 
125 /*
126  * @tc.name: NativeDrawingSurfaceTest_GetCanvas
127  * @tc.desc: test for GetCanvas.
128  * @tc.type: FUNC
129  * @tc.require: AR000GTO5R
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     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
149     OH_Drawing_SurfaceDestroy(surface_);
150     OH_Drawing_GpuContextDestroy(gpuContext_);
151 }
152 } // namespace Drawing
153 } // namespace Rosen
154 } // namespace OHOS