1/* 2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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, software 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 "EGL/egl.h" 17#include "EGL/eglext.h" 18#include "drawing_bitmap.h" 19#include "drawing_error_code.h" 20#include "drawing_gpu_context.h" 21#include "drawing_surface.h" 22#include "gtest/gtest.h" 23 24using namespace testing; 25using namespace testing::ext; 26 27namespace OHOS { 28namespace Rosen { 29namespace Drawing { 30class DrawingNativeSurFaceTest : public testing::Test { 31 public: 32 static void SetUpTestCase(); 33 static void TearDownTestCase(); 34 void SetUp() override; 35 void TearDown() override; 36 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 47void DrawingNativeSurFaceTest::SetUpTestCase() {} 48void DrawingNativeSurFaceTest::TearDownTestCase() {} 49void DrawingNativeSurFaceTest::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[] = { 61 EGL_SURFACE_TYPE, 62 EGL_WINDOW_BIT, 63 EGL_RED_SIZE, 64 8, 65 EGL_GREEN_SIZE, 66 8, 67 EGL_BLUE_SIZE, 68 8, 69 EGL_ALPHA_SIZE, 70 8, 71 EGL_RENDERABLE_TYPE, 72 EGL_OPENGL_ES3_BIT, 73 EGL_NONE, 74 }; 75 ret = eglChooseConfig(eglDisplay_, configAttribs, &eglConfig_, 1, &count); 76 EXPECT_EQ(ret, EGL_TRUE); 77 EXPECT_GE(count, 1); 78 79 const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; 80 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, contextAttribs); 81 EXPECT_NE(eglContext_, EGL_NO_CONTEXT); 82 83 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; 84 eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConfig_, attribs); 85 EXPECT_NE(eglSurface_, EGL_NO_SURFACE); 86 87 ret = eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_); 88 EXPECT_EQ(ret, EGL_TRUE); 89} 90 91void DrawingNativeSurFaceTest::TearDown() 92{ 93 EGLBoolean ret = eglDestroySurface(eglDisplay_, eglSurface_); 94 EXPECT_EQ(ret, EGL_TRUE); 95 96 ret = eglDestroyContext(eglDisplay_, eglContext_); 97 EXPECT_EQ(ret, EGL_TRUE); 98 99 ret = eglTerminate(eglDisplay_); 100 EXPECT_EQ(ret, EGL_TRUE); 101 102 eglSurface_ = EGL_NO_SURFACE; 103 eglContext_ = EGL_NO_CONTEXT; 104 eglDisplay_ = EGL_NO_DISPLAY; 105} 106 107/* 108 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0100 109 * @tc.name: testSurfaceCreateFromGpuContextNormal 110 * @tc.desc: test for testSurfaceCreateFromGpuContextNormal. 111 * @tc.size : SmallTest 112 * @tc.type : Function 113 * @tc.level : Level 0 114 */ 115HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateFromGpuContextNormal, TestSize.Level0) { 116 OH_Drawing_GpuContextOptions options{true}; 117 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 118 EXPECT_NE(gpuContext_, nullptr); 119 const int32_t width = 500; 120 const int32_t height = 500; 121 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 122 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo); 123 EXPECT_NE(surface_, nullptr); 124 OH_Drawing_SurfaceDestroy(surface_); 125 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo); 126 EXPECT_NE(surface_, nullptr); 127 OH_Drawing_SurfaceDestroy(surface_); 128} 129 130/* 131 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0101 132 * @tc.name: testSurfaceCreateFromGpuContextNull 133 * @tc.desc: test for testSurfaceCreateFromGpuContextNull. 134 * @tc.size : SmallTest 135 * @tc.type : Function 136 * @tc.level : Level 3 137 */ 138HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateFromGpuContextNull, TestSize.Level3) { 139 OH_Drawing_GpuContextOptions options{true}; 140 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 141 EXPECT_NE(gpuContext_, nullptr); 142 const int32_t width = 500; 143 const int32_t height = 500; 144 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 145 // OH_Drawing_GpuContext is NULL, check error code using OH_Drawing_ErrorCodeGet 146 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(nullptr, true, imageInfo); 147 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER); 148 // Exceptional parameter passing for OH_Drawing_Image_Info 149 OH_Drawing_Image_Info imageInfo2 = {0, 0, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 150 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo2); 151 EXPECT_EQ(surface_, nullptr); 152 OH_Drawing_SurfaceDestroy(surface_); 153} 154 155/* 156 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0102 157 * @tc.name: testSurfaceCreateFromGpuContextBoundary 158 * @tc.desc: test for testSurfaceCreateFromGpuContextBoundary. 159 * @tc.size : SmallTest 160 * @tc.type : Function 161 * @tc.level : Level 0 162 */ 163HWTEST_F(DrawingNativeSurFaceTest, testSurfaceCreateFromGpuContextBoundary, TestSize.Level0) { 164 OH_Drawing_GpuContextOptions options{true}; 165 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 166 EXPECT_NE(gpuContext_, nullptr); 167 const int32_t width = 4096; 168 const int32_t height = 2160; 169 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 170 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo); 171 EXPECT_NE(surface_, nullptr); 172 OH_Drawing_SurfaceDestroy(surface_); 173 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, false, imageInfo); 174 EXPECT_NE(surface_, nullptr); 175 OH_Drawing_SurfaceDestroy(surface_); 176} 177 178/* 179 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0200 180 * @tc.name: testSurfaceDestroyNormal 181 * @tc.desc: test for testSurfaceDestroyNormal. 182 * @tc.size : SmallTest 183 * @tc.type : Function 184 * @tc.level : Level 0 185 */ 186HWTEST_F(DrawingNativeSurFaceTest, testSurfaceDestroyNormal, TestSize.Level0) { 187 OH_Drawing_GpuContextOptions options{true}; 188 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 189 EXPECT_NE(gpuContext_, nullptr); 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 OH_Drawing_SurfaceDestroy(surface_); 196} 197 198/* 199 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0201 200 * @tc.name: testSurfaceDestroyNull 201 * @tc.desc: test for testSurfaceDestroyNull. 202 * @tc.size : SmallTest 203 * @tc.type : Function 204 * @tc.level : Level 3 205 */ 206HWTEST_F(DrawingNativeSurFaceTest, testSurfaceDestroyNull, TestSize.Level3) { 207 // free 208 OH_Drawing_SurfaceDestroy(nullptr); 209} 210 211/* 212 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0300 213 * @tc.name: testSurfaceGetCanvasNormal 214 * @tc.desc: test for testSurfaceGetCanvasNormal. 215 * @tc.size : SmallTest 216 * @tc.type : Function 217 * @tc.level : Level 0 218 */ 219HWTEST_F(DrawingNativeSurFaceTest, testSurfaceGetCanvasNormal, TestSize.Level0) { 220 OH_Drawing_GpuContextOptions options{true}; 221 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 222 EXPECT_NE(gpuContext_, nullptr); 223 // 1. OH_Drawing_SurfaceCreateFromGpuContext 224 const int32_t width = 500; 225 const int32_t height = 500; 226 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 227 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo); 228 EXPECT_NE(surface_, nullptr); 229 // 2. OH_Drawing_SurfaceGetCanvas, get the canvas object from the surface object, a pointer to the surface object, 230 // and call the drawing interface 231 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_); 232 EXPECT_NE(canvas_, nullptr); 233 // 3. Free memory 234 OH_Drawing_SurfaceDestroy(surface_); 235} 236 237/* 238 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0301 239 * @tc.name: testSurfaceGetCanvasNull 240 * @tc.desc: test for testSurfaceGetCanvasNull. 241 * @tc.size : SmallTest 242 * @tc.type : Function 243 * @tc.level : Level 3 244 */ 245HWTEST_F(DrawingNativeSurFaceTest, testSurfaceGetCanvasNull, TestSize.Level3) { 246 OH_Drawing_GpuContextOptions options{true}; 247 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 248 EXPECT_NE(gpuContext_, nullptr); 249 // 1. OH_Drawing_SurfaceCreateFromGpuContext 250 const int32_t width = 500; 251 const int32_t height = 500; 252 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 253 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo); 254 EXPECT_NE(surface_, nullptr); 255 // 2. OH_Drawing_SurfaceGetCanvas with null parameter, check error code using OH_Drawing_ErrorCodeGet 256 canvas_ = OH_Drawing_SurfaceGetCanvas(nullptr); 257 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER); 258 OH_Drawing_SurfaceDestroy(surface_); 259} 260 261/* 262 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SURFACE_0302 263 * @tc.name: testSurfaceGetCanvasBoundary 264 * @tc.desc: test for testSurfaceGetCanvasBoundary. 265 * @tc.size : SmallTest 266 * @tc.type : Function 267 * @tc.level : Level 0 268 */ 269HWTEST_F(DrawingNativeSurFaceTest, testSurfaceGetCanvasBoundary, TestSize.Level0) { 270 OH_Drawing_GpuContextOptions options{true}; 271 gpuContext_ = OH_Drawing_GpuContextCreateFromGL(options); 272 EXPECT_NE(gpuContext_, nullptr); 273 // 1. OH_Drawing_SurfaceCreateFromGpuContext 274 const int32_t width = 4096; 275 const int32_t height = 2160; 276 OH_Drawing_Image_Info imageInfo = {width, height, COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; 277 surface_ = OH_Drawing_SurfaceCreateFromGpuContext(gpuContext_, true, imageInfo); 278 EXPECT_NE(surface_, nullptr); 279 // 2. OH_Drawing_SurfaceGetCanvas, get the canvas object from the surface object, a pointer to the surface object, 280 // and call the drawing interface 281 canvas_ = OH_Drawing_SurfaceGetCanvas(surface_); 282 EXPECT_NE(canvas_, nullptr); 283 // 3. Free memory 284 OH_Drawing_SurfaceDestroy(surface_); 285} 286} // namespace Drawing 287} // namespace Rosen 288} // namespace OHOS