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 #include "src/core/SkPathComplexityDfx.h"
17 #include "src/core/SkTraceEvent.h"
18 
19 #ifdef SK_ENABLE_PATH_COMPLEXITY_DFX
20 constexpr int PATH_TRACE_LEVEL = 1;
21 
GetDebugTraceLevel()22 int GetDebugTraceLevel()
23 {
24     static int openDebugTraceLevel =
25         std::atoi((OHOS::system::GetParameter("persist.sys.graphic.openDebugTrace", "0")).c_str());
26     return openDebugTraceLevel;
27 }
28 
IsShowPathComplexityEnabled()29 bool IsShowPathComplexityEnabled()
30 {
31     static bool enabled =
32         std::atoi((OHOS::system::GetParameter("persist.sys.graphic.showPathComplexity", "0")).c_str()) != 0;
33     return enabled;
34 }
35 
36 
AddPathComplexityTrace(SkScalar complexity)37 void SkPathComplexityDfx::AddPathComplexityTrace(SkScalar complexity)
38 {
39     if (GetDebugTraceLevel() >= PATH_TRACE_LEVEL) {
40         HITRACE_OHOS_NAME_FMT_ALWAYS("Path Complexity Debug: %f", complexity);
41     }
42 }
ShowPathComplexityDfx(SkCanvas* canvas, const SkPath& path)43 void SkPathComplexityDfx::ShowPathComplexityDfx(SkCanvas* canvas, const SkPath& path)
44 {
45     if (!canvas) {
46         return;
47     }
48 
49     if (IsShowPathComplexityEnabled()) {
50         constexpr size_t MESSAGE_SIZE = 4;
51         constexpr SkScalar MESSAGE_FONT_SIZE = 30.0f;
52         constexpr SkScalar MARGIN_LENGTH = 10.0f;
53 
54         SkScalar complexity = 0.f;
55         SkScalar avgLength = 0.f;
56         compute_complexity(path, avgLength, complexity);
57         std::string message = std::to_string(complexity).substr(0, MESSAGE_SIZE);
58 
59         SkFont tempFont;
60         tempFont.setSize(MESSAGE_FONT_SIZE / (std::abs(canvas->getTotalMatrix().get(0)) + 1e-3));
61         SkPaint tempPaint;
62         tempPaint.setColor(SK_ColorRED);
63         canvas->drawSimpleText(message.c_str(), MESSAGE_SIZE, SkTextEncoding::kUTF8,
64             0, MARGIN_LENGTH, tempFont, tempPaint);
65     }
66 }
67 #endif
68