1 /* 2 * Copyright (c) 2020-2021 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 #include "common/graphic_startup.h" 17 #include "common/task_manager.h" 18 #include "components/root_view.h" 19 #include "components/ui_image_view.h" 20 #include "components/ui_label.h" 21 #include "components/ui_texture_mapper.h" 22 #include "components/ui_view_group.h" 23 #include "dock/screen_device.h" 24 #include "dock/screen_device_proxy.h" 25 #include "gfx_utils/style.h" 26 #include "test_resource_config.h" 27 #include "window/window.h" 28 29 #include <climits> 30 #include <gtest/gtest.h> 31 32 using namespace testing::ext; 33 namespace OHOS { 34 class TestDevice : public ScreenDevice { 35 public: TestDevice()36 TestDevice() {} ~TestDevice()37 virtual ~TestDevice() {} 38 HardwareFill(const Rect & fillArea,uint32_t color,OpacityType opa,uint8_t * dst,uint32_t dstStride,ColorMode dstColorMode)39 bool HardwareFill(const Rect& fillArea, 40 uint32_t color, 41 OpacityType opa, 42 uint8_t* dst, 43 uint32_t dstStride, 44 ColorMode dstColorMode) override 45 { 46 fillIn_ = true; 47 return false; 48 } 49 HardwareBlend(const uint8_t * src,const Rect & srcRect,uint32_t srcStride,uint32_t srcLineNumber,ColorMode srcColorMode,uint32_t color,OpacityType opa,uint8_t * dst,uint32_t dstStride,ColorMode dstColorMode,uint32_t x,uint32_t y)50 bool HardwareBlend(const uint8_t* src, 51 const Rect& srcRect, 52 uint32_t srcStride, 53 uint32_t srcLineNumber, 54 ColorMode srcColorMode, 55 uint32_t color, 56 OpacityType opa, 57 uint8_t* dst, 58 uint32_t dstStride, 59 ColorMode dstColorMode, 60 uint32_t x, 61 uint32_t y) override 62 { 63 blendIn_ = true; 64 return false; 65 } 66 HardwareTransform(const uint8_t * src,ColorMode srcColorMode,const Rect & srcRect,const Matrix3<float> & transformMatrix,OpacityType opa,uint32_t color,const Rect & mask,uint8_t * dst,uint32_t dstStride,ColorMode dstColorMode,const TransformOption & option)67 bool HardwareTransform(const uint8_t* src, 68 ColorMode srcColorMode, 69 const Rect& srcRect, 70 const Matrix3<float>& transformMatrix, 71 OpacityType opa, 72 uint32_t color, 73 const Rect& mask, 74 uint8_t* dst, 75 uint32_t dstStride, 76 ColorMode dstColorMode, 77 const TransformOption& option) override 78 { 79 transformIn_ = true; 80 return false; 81 } 82 83 bool fillIn_ = false; 84 bool blendIn_ = false; 85 bool transformIn_ = false; 86 }; 87 88 static TestDevice* g_testDevice = nullptr; 89 90 class HardwareAccelerationTest : public testing::Test { 91 public: SetUpTestCase(void)92 static void SetUpTestCase(void) 93 { 94 GraphicStartUp::Init(); 95 g_testDevice = new TestDevice; 96 ScreenDeviceProxy::GetInstance()->SetDevice(g_testDevice); 97 } TearDownTestCase(void)98 static void TearDownTestCase(void) 99 { 100 delete g_testDevice; 101 g_testDevice = nullptr; 102 ScreenDeviceProxy::GetInstance()->SetDevice(nullptr); 103 } SetUp()104 void SetUp() 105 { 106 g_testDevice->fillIn_ = false; 107 g_testDevice->blendIn_ = false; 108 g_testDevice->transformIn_ = false; 109 } TearDown()110 void TearDown() {} 111 CreateDefaultWindow(RootView * rootView,int x,int y)112 static void CreateDefaultWindow(RootView* rootView, int x, int y) 113 { 114 WindowConfig config = {}; 115 config.rect = rootView->GetRect(); 116 config.rect.SetPosition(x, y); 117 Window* window = Window::CreateWindow(config); 118 if (window != nullptr) { 119 window->BindRootView(rootView); 120 } 121 } 122 }; 123 124 /** 125 * @tc.name: HardwareFill_001 126 * @tc.desc: Verify HardwareFill function, equal. 127 * Need set ENABLE_HARDWARE_ACCELERATION to 1. 128 * @tc.type: FUNC 129 * @tc.require: SR000ERCR6 130 */ 131 #if ENABLE_HARDWARE_ACCELERATION 132 HWTEST_F(HardwareAccelerationTest, HardwareFill_001, TestSize.Level1) 133 { 134 RootView* rootView = RootView::GetInstance(); 135 rootView->SetWidth(600); // 600: width 136 rootView->SetHeight(300); // 300: height 137 rootView->SetPosition(0, 0); 138 rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Olive().full); 139 rootView->Invalidate(); 140 141 HardwareAccelerationTest::CreateDefaultWindow(rootView, 0, 0); 142 143 TaskManager::GetInstance()->TaskHandler(); 144 EXPECT_EQ(g_testDevice->fillIn_, true); 145 Window::DestoryWindow(rootView->GetBoundWindow()); 146 } 147 #endif 148 149 /** 150 * @tc.name: HardwareBlend_001 151 * @tc.desc: Verify HardwareBlend function, equal. 152 * Need set ENABLE_HARDWARE_ACCELERATION to 1. 153 * @tc.type: FUNC 154 * @tc.require: AR000EVI3I 155 */ 156 #if ENABLE_HARDWARE_ACCELERATION 157 HWTEST_F(HardwareAccelerationTest, HardwareBlend_001, TestSize.Level0) 158 { 159 RootView* rootView = RootView::GetInstance(); 160 rootView->SetWidth(600); // 600: width 161 rootView->SetHeight(300); // 300: height 162 rootView->SetPosition(0, 0); 163 rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Olive().full); 164 165 UIImageView* imageView = new UIImageView; 166 imageView->SetPosition(200, 50, 200, 200); // 200: x; 50: y; 200: width; 200: height 167 imageView->SetSrc(RED_IMAGE_PATH); 168 169 rootView->Add(imageView); 170 rootView->Invalidate(); 171 172 HardwareAccelerationTest::CreateDefaultWindow(rootView, 0, 0); 173 174 TaskManager::GetInstance()->TaskHandler(); 175 EXPECT_EQ(g_testDevice->blendIn_, true); 176 Window::DestoryWindow(rootView->GetBoundWindow()); 177 } 178 #endif 179 180 /** 181 * @tc.name: HardwareBlend_002 182 * @tc.desc: Verify HardwareBlend function for text, equal. 183 * Need set ENABLE_HARDWARE_ACCELERATION and ENABLE_HARDWARE_ACCELERATION_FOR_TEXT to 1. 184 * @tc.type: FUNC 185 * @tc.require: AR000EVI3I 186 */ 187 #if ENABLE_HARDWARE_ACCELERATION && ENABLE_HARDWARE_ACCELERATION_FOR_TEXT 188 HWTEST_F(HardwareAccelerationTest, HardwareBlend_002, TestSize.Level1) 189 { 190 RootView* rootView = RootView::GetInstance(); 191 rootView->SetWidth(600); // 600: width 192 rootView->SetHeight(300); // 300: height 193 rootView->SetPosition(0, 0); 194 rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Olive().full); 195 196 UILabel* label = new UILabel(); 197 // 288: x-coordinate; 48: y-coordinate 198 label->SetPosition(0, 0, 288, 48); 199 label->SetText("HardwareBlend_002 测试"); 200 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18); // 18 : font size 201 202 rootView->Add(label); 203 rootView->Invalidate(); 204 205 HardwareAccelerationTest::CreateDefaultWindow(rootView, 0, 0); 206 207 TaskManager::GetInstance()->TaskHandler(); 208 EXPECT_EQ(g_testDevice->blendIn_, true); 209 Window::DestoryWindow(rootView->GetBoundWindow()); 210 } 211 #endif 212 213 /** 214 * @tc.name: HardwareTransform_001 215 * @tc.desc: Verify HardwareTransform function, equal. 216 * Need set ENABLE_HARDWARE_ACCELERATION to 1. 217 * @tc.type: FUNC 218 * @tc.require: AR000EVI3I 219 */ 220 #if ENABLE_HARDWARE_ACCELERATION 221 HWTEST_F(HardwareAccelerationTest, HardwareTransform_001, TestSize.Level0) 222 { 223 RootView* rootView = RootView::GetInstance(); 224 rootView->SetWidth(600); // 600: width 225 rootView->SetHeight(300); // 300: height 226 rootView->SetPosition(0, 0); 227 rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Olive().full); 228 229 UIImageView* imageView = new UIImageView; 230 imageView->SetPosition(200, 50, 200, 200); // 200: x; 50: y; 200: width; 200: height 231 imageView->SetSrc(RED_IMAGE_PATH); 232 Rect viewRect = imageView->GetOrigRect(); 233 TransformMap transMap(viewRect); 234 Vector2<float> pivot_(58, 58); // 58:x value 58:y value 235 transMap.Rotate(90, pivot_); // 90:degree 236 transMap.Scale(Vector2<float>(1.5, 1.5), pivot_); // 1.5:x scale 1.5:y scale 237 imageView->SetTransformMap(transMap); 238 239 rootView->Add(imageView); 240 rootView->Invalidate(); 241 242 HardwareAccelerationTest::CreateDefaultWindow(rootView, 0, 0); 243 244 TaskManager::GetInstance()->TaskHandler(); 245 EXPECT_EQ(g_testDevice->transformIn_, true); 246 Window::DestoryWindow(rootView->GetBoundWindow()); 247 } 248 #endif 249 250 /** 251 * @tc.name: HardwareTransform_002 252 * @tc.desc: Verify HardwareTransform function in abnormal scene, equal. 253 * Need set ENABLE_HARDWARE_ACCELERATION to 1. 254 * @tc.type: FUNC 255 * @tc.require: AR000EVI3I 256 */ 257 #if ENABLE_HARDWARE_ACCELERATION 258 HWTEST_F(HardwareAccelerationTest, HardwareTransform_002, TestSize.Level1) 259 { 260 // set device to null 261 ScreenDeviceProxy::GetInstance()->SetDevice(nullptr); 262 263 RootView* rootView = RootView::GetInstance(); 264 rootView->SetWidth(600); // 600: width 265 rootView->SetHeight(300); // 300: height 266 rootView->SetPosition(0, 0); 267 rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Olive().full); 268 269 UIImageView* imageView = new UIImageView; 270 imageView->SetPosition(200, 50, 200, 200); // 200: x; 50: y; 200: width; 200: height 271 imageView->SetSrc(RED_IMAGE_PATH); 272 Rect viewRect = imageView->GetOrigRect(); 273 TransformMap transMap(viewRect); 274 Vector2<float> pivot_(58, 58); // 58:x value 58:y value 275 transMap.Rotate(90, pivot_); // 90:degree 276 transMap.Scale(Vector2<float>(1.5, 1.5), pivot_); // 1.5:x scale 1.5:y scale 277 imageView->SetTransformMap(transMap); 278 279 rootView->Add(imageView); 280 rootView->Invalidate(); 281 282 HardwareAccelerationTest::CreateDefaultWindow(rootView, 0, 0); 283 284 TaskManager::GetInstance()->TaskHandler(); 285 EXPECT_EQ(g_testDevice->fillIn_, false); 286 EXPECT_EQ(g_testDevice->blendIn_, false); 287 EXPECT_EQ(g_testDevice->transformIn_, false); 288 Window::DestoryWindow(rootView->GetBoundWindow()); 289 } 290 #endif 291 } // namespace OHOS