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 #include "test_base.h"
16 
17 #include <sstream>
18 #include <string>
19 
20 #include "test_common.h"
21 #include "image/image.h"
22 #include "utils/log.h"
23 
24 namespace OHOS {
25 namespace Rosen {
SetCanvas(TestDisplayCanvas * canvas)26 void TestBase::SetCanvas(TestDisplayCanvas* canvas)
27 {
28     playbackCanvas_ = canvas;
29 }
CreateBitmapCanvas()30 int TestBase::CreateBitmapCanvas()
31 {
32     OHOS::Media::InitializationOptions opts;
33     opts.size.width = width_;
34     opts.size.height = height_;
35     opts.editable = true;
36     std::unique_ptr<OHOS::Media::PixelMap> pixelMap = OHOS::Media::PixelMap::Create(opts);
37     pixelMap_.reset(pixelMap.get());
38     pixelMap.release();
39     if (pixelMap_ == nullptr) {
40         TestCommon::Log("failed to create pixelmap");
41         return RET_FAILED;
42     }
43     Drawing::BitmapFormat format {Drawing::COLORTYPE_RGBA_8888, Drawing::ALPHATYPE_OPAQUE};
44     bitmap_.Build(width_, height_, format);
45     OHOS::Media::ImageInfo imageInfo;
46     pixelMap_->GetImageInfo(imageInfo);
47     Drawing::ImageInfo drawingImageInfo { imageInfo.size.width, imageInfo.size.height,
48         TestCommon::PixelFormatToDrawingColorType(imageInfo.pixelFormat),
49         TestCommon::AlphaTypeToDrawingAlphaType(imageInfo.alphaType),
50         TestCommon::ColorSpaceToDrawingColorSpace(imageInfo.colorSpace) };
51     bitmap_.SetInfo(drawingImageInfo);
52     void* pixel = const_cast<void*>(reinterpret_cast<const void*>(pixelMap_->GetPixels()));
53     if (pixel == nullptr) {
54         TestCommon::Log("failed to GetPixels");
55         return RET_FAILED;
56     }
57     bitmap_.SetPixels(pixel);
58     bitmapCanvas_ = std::make_shared<Drawing::Canvas>();
59     if (bitmapCanvas_ == nullptr) {
60         TestCommon::Log("failed to create bitmapCanvas");
61         return RET_FAILED;
62     }
63     bitmapCanvas_->Bind(bitmap_);
64     bitmapCanvas_->DrawColor(background_);
65     return RET_OK;
66 }
DiasplayToScreen()67 int TestBase::DiasplayToScreen()
68 {
69     if (playbackCanvas_ == nullptr) {
70         TestCommon::Log("playbackCanvas_ is null ");
71         return RET_FAILED;
72     }
73     playbackCanvas_->DrawColor(0xff000000); //0xff000000 black
74     playbackCanvas_->DrawBitmap(bitmap_, 0, 0);
75     return RET_OK;
76 }
SetFileName(std::string fileName)77 void TestBase::SetFileName(std::string fileName)
78 {
79     fileName_ = fileName;
80 }
SetTestCount(uint32_t count)81 void TestBase::SetTestCount(uint32_t count)
82 {
83     testCount_ = count;
84 }
TestFunctionCpu()85 void TestBase::TestFunctionCpu()
86 {
87     if (CreateBitmapCanvas() != RET_OK) {
88         return;
89     }
90     OnTestFunctionCpu(bitmapCanvas_.get());
91     (void)DiasplayToScreen();
92     (void)TestCommon::PackingPixmap(pixelMap_, fileName_);
93 }
TestPerformanceCpu()94 void TestBase::TestPerformanceCpu()
95 {
96     if (CreateBitmapCanvas() != RET_OK) {
97         return;
98     }
99     LogStart();
100     OnTestPerformanceCpu(bitmapCanvas_.get());
101     LogEnd();
102     (void)DiasplayToScreen();
103 }
TestFunctionGpuUpScreen()104 void TestBase::TestFunctionGpuUpScreen()
105 {
106     if (playbackCanvas_ == nullptr) {
107         TestCommon::Log("playbackCanvas_ is null ");
108         return;
109     }
110     OnTestFunctionGpuUpScreen(playbackCanvas_);
111 }
TestPerformanceGpuUpScreen()112 void TestBase::TestPerformanceGpuUpScreen()
113 {
114     if (playbackCanvas_ == nullptr) {
115         TestCommon::Log("playbackCanvas_ is null ");
116         return;
117     }
118     LogStart();
119     OnTestPerformanceGpuUpScreen(playbackCanvas_);
120     LogEnd();
121 }
LogStart()122 void TestBase::LogStart()
123 {
124     timeStart_ = std::chrono::high_resolution_clock::now();
125 }
LogEnd()126 void TestBase::LogEnd()
127 {
128     std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
129     int time = std::chrono::duration_cast<std::chrono::milliseconds>(end - timeStart_).count();
130     std::ostringstream stream;
131     stream << "DrawingApiTest TotalApiCallTime: [" << time << "]\n";
132     stream << "DrawingApiTest TotalApiCallCount: [" << testCount_ << "]";
133     TestCommon::Log(stream.str());
134 }
135 } // namespace Rosen
136 } // namespace OHOS