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, 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 #ifndef TEST_BASE_H 17 #define TEST_BASE_H 18 19 #include <bits/alltypes.h> 20 #include <chrono> 21 #include <native_drawing/drawing_canvas.h> 22 #include <native_drawing/drawing_gpu_context.h> 23 #include <native_drawing/drawing_bitmap.h> 24 #include <native_drawing/drawing_color.h> 25 #include <native_drawing/drawing_surface.h> 26 #include <string> 27 #include "napi/native_api.h" 28 #include <multimedia/image_framework/image/pixelmap_native.h> 29 30 #define DEFAULT_WIDTH 720 31 #define DEFAULT_HEIGHT 720 32 #define DEFAULT_TESTCOUNT 1 33 #define DEFAULT_BACKGROUND_COLOR 0xFFFFFFFF 34 35 class TestBase { 36 public: TestBase()37 TestBase(){}; TestBase(int styleType)38 explicit TestBase(int styleType):styleType_(styleType){}; ~TestBase()39 virtual ~TestBase() 40 { 41 if (dstPixels_) { 42 free(dstPixels_); 43 dstPixels_ = nullptr; 44 } 45 StyleSettingsDestroy(nullptr); 46 }; 47 48 void SetFileName(std::string fileName); 49 void SetTestCount(uint32_t testCount); 50 OH_Drawing_Bitmap* GetBitmap(); 51 uint32_t GetTime(); 52 void Destroy(); 53 54 void ClipCanvasToDrawSize(OH_Drawing_Canvas *canvas); 55 void TestFunctionCpu(napi_env env); 56 void TestFunctionGpu(OH_Drawing_Canvas* canvas); 57 void TestFunctionGpu(napi_env env); 58 void TestPerformanceCpu(napi_env env); 59 void TestPerformanceGpu(OH_Drawing_Canvas* canvas); 60 void TestPerformanceGpu(napi_env env); 61 void TestStabilityCpu(); 62 // CPU drawing function test, save drawing results to image (filename_) OnTestFunction(OH_Drawing_Canvas * canvas)63 virtual void OnTestFunction(OH_Drawing_Canvas* canvas) {}; 64 // CPU drawing performance test, execute critical interface testCount_ times repeatedly OnTestPerformance(OH_Drawing_Canvas * canvas)65 virtual void OnTestPerformance(OH_Drawing_Canvas* canvas) {}; 66 // CPU drawing stability test OnTestStability(OH_Drawing_Canvas * canvas)67 virtual void OnTestStability(OH_Drawing_Canvas* canvas) {}; 68 69 enum { // 公共的pen,brush,filter等配置,在执行性能用例前设置 70 DRAW_STYLE_NONE = 0, // 无配置 71 DRAW_STYLE_COMPLEX, // 保证性能测试稳定性: 绘制一个红色填充 72 }; 73 int styleType_ = DRAW_STYLE_NONE; 74 void StyleSettings(OH_Drawing_Canvas* canvas, int32_t type); 75 void StyleSettingsDestroy(OH_Drawing_Canvas *canvas); 76 77 uint32_t GetBitmapWidth(); 78 uint32_t GetBitmapHeight(); 79 80 protected: 81 // cpu bitmap canvas 82 void CreateBitmapCanvas(); 83 void CreateGpuCanvas(); 84 void BitmapCanvasToFile(); 85 void GpuCanvasToFile(napi_env env, bool saveFile = true); 86 void Pixmap2RawFile(void *pixelmap, uint32_t pixelMapSize); 87 void Pixmap2ImageFile(OH_PixelmapNative* pixelMap); 88 89 unsigned long long LogStart(); 90 void LogEnd(unsigned long long longstart); 91 92 void* dstPixels_ = nullptr; 93 94 //pixmap to file 95 OH_Drawing_Bitmap* bitmap_ = nullptr; 96 OH_Drawing_Canvas* bitmapCanvas_ = nullptr; 97 OH_Drawing_Canvas *gpuCanvas_ = nullptr; 98 OH_Drawing_GpuContext *gpuContext_ = nullptr; 99 OH_Drawing_Surface *surface_ = nullptr; 100 OH_Drawing_Image_Info imageInfo_; 101 102 // bitmapcanvas cfg 103 uint32_t bitmapWidth_ = DEFAULT_WIDTH; 104 uint32_t bitmapHeight_ = DEFAULT_HEIGHT; 105 uint32_t background_ = DEFAULT_BACKGROUND_COLOR; 106 uint32_t backgroundA_ = DEFAULT_BACKGROUND_COLOR; 107 uint32_t backgroundR_ = DEFAULT_BACKGROUND_COLOR; 108 uint32_t backgroundG_ = DEFAULT_BACKGROUND_COLOR; 109 uint32_t backgroundB_ = DEFAULT_BACKGROUND_COLOR; 110 std::string fileName_ = "default"; 111 uint32_t testCount_ = DEFAULT_TESTCOUNT; 112 uint32_t usedTime_ = 0; 113 114 OH_Drawing_Brush* styleBrush_ = nullptr; 115 OH_Drawing_Pen* stylePen_ = nullptr; 116 OH_Drawing_MaskFilter* styleMask_ = nullptr; 117 OH_Drawing_Filter* styleFilter_ = nullptr; 118 OH_Drawing_Point* styleCenter_ = nullptr; 119 OH_Drawing_ShaderEffect* styleEffect_ = nullptr; 120 OH_Drawing_PathEffect* stylePathEffect_ = nullptr; 121 }; 122 123 #endif // TEST_BASE_H