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