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 "image_dfx.h"
17
18 #include <unistd.h>
19 #include <fstream>
20 #include <chrono>
21
22 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
23 #include <sys/timerfd.h>
24 #include "ffrt.h"
25 #include "hisysevent.h"
26 #endif
27
28 #include "image_utils.h"
29 #include "image_log.h"
30
31 namespace OHOS {
32 namespace Media {
33 static constexpr char IMAGE_FWK_UE[] = "IMAGE_FWK_UE";
34 const static std::string DEFAULT_VERSION_ID = "1";
35
ImageEvent()36 ImageEvent::ImageEvent()
37 {
38 startTime_ = ImageUtils::GetNowTimeMilliSeconds();
39 }
40
~ImageEvent()41 ImageEvent::~ImageEvent()
42 {
43 if (!options_.errorMsg.empty()) {
44 ReportDecodeFault();
45 } else {
46 ReportDecodeInfo();
47 }
48 }
49
SetDecodeInfoOptions(const DecodeInfoOptions & options)50 void ImageEvent::SetDecodeInfoOptions(const DecodeInfoOptions &options)
51 {
52 options_ = options;
53 }
54
GetDecodeInfoOptions()55 DecodeInfoOptions& ImageEvent::GetDecodeInfoOptions()
56 {
57 return options_;
58 }
59
SetDecodeErrorMsg(std::string msg)60 void ImageEvent::SetDecodeErrorMsg(std::string msg)
61 {
62 options_.errorMsg = msg;
63 }
64
getInvokeType()65 std::string ImageEvent::getInvokeType()
66 {
67 std::string invokeType;
68 switch (options_.invokeType) {
69 case (JS_INTERFACE):
70 invokeType = "js_interface";
71 break;
72 case (C_INTERFACE):
73 invokeType = "c_interface";
74 break;
75 default:
76 invokeType = "inner_interface";
77 }
78 return invokeType;
79 }
80
ReportDecodeFault()81 void ImageEvent::ReportDecodeFault()
82 {
83 std::string temp = getInvokeType();
84 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
85 int timerFd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
86 if (timerFd < 0) {
87 return;
88 }
89 close(timerFd);
90 DecodeInfoOptions options = options_;
91 ffrt::submit([options, temp] {
92 std::string packageName = ImageUtils::GetCurrentProcessName();
93 HiSysEventWrite(IMAGE_FWK_UE,
94 "DECODE_FAULT",
95 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
96 "PNAMEID", packageName,
97 "PVERSIONID", DEFAULT_VERSION_ID,
98 "APPLICATION_NAME", packageName,
99 "ROTATE", options.rotate,
100 "EDITABLE", options.editable,
101 "SAMPLE_SIZE", options.sampleSize,
102 "SOURCE_WIDTH", options.sourceWidth,
103 "SOURCE_HEIGHT", options.sourceHeight,
104 "DESIRE_SIZE_WIDTH", options.desireSizeWidth,
105 "DESIRE_SIZE_HEIGHT", options.desireSizeHeight,
106 "DESIRE_REGION_WIDTH", options.desireRegionWidth,
107 "DESIRE_REGION_HEIGHT", options.desireRegionHeight,
108 "DESIRE_REGION_X", options.desireRegionX,
109 "DESIRE_REGION_Y", options.desireRegionY,
110 "DESIRE_DESIRE_PIXEL_FORMAT", options.desirePixelFormat,
111 "INDEX", options.index,
112 "FIT_DENSITY", options.fitDensity,
113 "DESIRE_COLOR_SPACE", options.desireColorSpace,
114 "MIMETYPE", options.mimeType,
115 "MEMORY_SIZE", options.memorySize,
116 "MEMORY_TYPE", options.memoryType,
117 "IMAGE_SOURCE", options.imageSource,
118 "INVOKE_TYPE", temp,
119 "INCREMENTAL_DECODE", options.isIncrementalDecode,
120 "HARD_DECODE", options.isHardDecode,
121 "HARD_DECODE_ERROR", options.hardDecodeError,
122 "ERROR_MSG", options.errorMsg,
123 "PLUGIN_TYPE", options.pluginType,
124 "HEIF_GRID_WIDTH", options.heifGridWidth,
125 "HEIF_GRID_HEIGHT", options.heifGridHeight);
126 }, {}, {});
127 #endif
128 }
129
GetInvokeTypeStr(uint16_t invokeType)130 std::string GetInvokeTypeStr(uint16_t invokeType)
131 {
132 switch (invokeType) {
133 case (JS_INTERFACE):
134 return "js_interface";
135 case (C_INTERFACE):
136 return "c_interface";
137 default:
138 return "inner_interface";
139 }
140 }
141
ReportDecodeInfo()142 void ImageEvent::ReportDecodeInfo()
143 {
144 uint64_t costTime = ImageUtils::GetNowTimeMilliSeconds() - startTime_;
145 std::string temp = GetInvokeTypeStr(options_.invokeType);
146 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
147 int timerFd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
148 if (timerFd < 0) {
149 return;
150 }
151 close(timerFd);
152 DecodeInfoOptions options = options_;
153 ffrt::submit([options, temp, costTime] {
154 std::string packageName = ImageUtils::GetCurrentProcessName();
155 HiSysEventParam params[] = {
156 { .name = "PNAMEID", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char*>(packageName.c_str()) } },
157 { .name = "PVERSIONID", .t = HISYSEVENT_STRING,
158 .v = { .s = const_cast<char*>(DEFAULT_VERSION_ID.c_str()) } },
159 { .name = "APPLICATION_NAME", .t = HISYSEVENT_STRING,
160 .v = { .s = const_cast<char*>(packageName.c_str()) } },
161 { .name = "ROTATE", .t = HISYSEVENT_FLOAT, .v = { .f = options.rotate } },
162 { .name = "EDITABLE", .t = HISYSEVENT_BOOL, .v = { .b = options.editable } },
163 { .name = "SAMPLE_SIZE", .t = HISYSEVENT_UINT32, .v = { .ui32 = options.sampleSize } },
164 { .name = "SOURCE_WIDTH", .t = HISYSEVENT_INT32, .v = { .i32 = options.sourceWidth } },
165 { .name = "SOURCE_HEIGHT", .t = HISYSEVENT_INT32, .v = { .i32 = options.sourceHeight } },
166 { .name = "DESIRE_SIZE_WIDTH", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireSizeWidth } },
167 { .name = "DESIRE_SIZE_HEIGHT", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireSizeHeight } },
168 { .name = "DESIRE_REGION_WIDTH", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireRegionWidth } },
169 { .name = "DESIRE_REGION_HEIGHT", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireRegionHeight } },
170 { .name = "DESIRE_REGION_X", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireRegionX } },
171 { .name = "DESIRE_REGION_Y", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireRegionY } },
172 { .name = "DESIRE_DESIRE_PIXEL_FORMAT", .t = HISYSEVENT_INT32, .v = { .i32 = options.desirePixelFormat } },
173 { .name = "INDEX", .t = HISYSEVENT_UINT32, .v = { .ui32 = options.index } },
174 { .name = "FIT_DENSITY", .t = HISYSEVENT_INT32, .v = { .i32 = options.fitDensity } },
175 { .name = "DESIRE_COLOR_SPACE", .t = HISYSEVENT_INT32, .v = { .i32 = options.desireColorSpace } },
176 { .name = "MIMETYPE", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char*>(options.mimeType.c_str()) } },
177 { .name = "MEMORY_SIZE", .t = HISYSEVENT_UINT32, .v = { .ui32 = options.memorySize } },
178 { .name = "MEMORY_TYPE", .t = HISYSEVENT_INT32, .v = { .i32 = options.memoryType } },
179 { .name = "IMAGE_SOURCE", .t = HISYSEVENT_STRING,
180 .v = { .s = const_cast<char*>(options.imageSource.c_str()) } },
181 { .name = "INVOKE_TYPE", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char*>(temp.c_str()) } },
182 { .name = "INCREMENTAL_DECODE", .t = HISYSEVENT_BOOL, .v = { .b = options.isIncrementalDecode } },
183 { .name = "HARD_DECODE", .t = HISYSEVENT_BOOL, .v = { .b = options.isHardDecode } },
184 { .name = "HARD_DECODE_ERROR", .t = HISYSEVENT_STRING,
185 .v = { .s = const_cast<char*>(options.hardDecodeError.c_str()) } },
186 { .name = "COST_TIME", .t = HISYSEVENT_UINT64, .v = { .ui64 = costTime } },
187 { .name = "PLUGIN_TYPE", .t = HISYSEVENT_STRING,
188 .v = { .s = const_cast<char*>(options.pluginType.c_str()) } },
189 { .name = "HEIF_GRID_WIDTH", .t = HISYSEVENT_UINT32, .v = { .ui32 = options.heifGridWidth } },
190 { .name = "HEIF_GRID_HEIGHT", .t = HISYSEVENT_UINT32, .v = { .ui32 = options.heifGridHeight } },
191 };
192 OH_HiSysEvent_Write(IMAGE_FWK_UE, "DECODE_INFORMATION", HISYSEVENT_BEHAVIOR, params,
193 sizeof(params) / sizeof(params[0]));
194 }, {}, {});
195 #endif
196 }
197
ReportCreateImageSourceFault(uint32_t width,uint32_t height,std::string type,std::string message)198 void ReportCreateImageSourceFault(uint32_t width, uint32_t height, std::string type, std::string message)
199 {
200 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
201 std::string packageName = ImageUtils::GetCurrentProcessName();
202 HiSysEventWrite(IMAGE_FWK_UE,
203 "CREATE_IMAGESOURCE_FAULT",
204 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
205 "PNAMEID", packageName,
206 "PVERSIONID", DEFAULT_VERSION_ID,
207 "WIDTH", width,
208 "HEIGHT", height,
209 "TYPE", type,
210 "ERROR_MSG", message);
211 #endif
212 }
213
ReportEncodeFault(uint32_t width,uint32_t height,std::string mimeType,std::string message)214 void ReportEncodeFault(uint32_t width, uint32_t height, std::string mimeType, std::string message)
215 {
216 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
217 std::string packageName = ImageUtils::GetCurrentProcessName();
218 HiSysEventWrite(IMAGE_FWK_UE,
219 "ENCODE_FAULT",
220 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
221 "PNAMEID", packageName,
222 "PVERSIONID", DEFAULT_VERSION_ID,
223 "WIDTH", width,
224 "HEIGHT", height,
225 "MIME_TYPE", mimeType,
226 "ERROR_MSG", message);
227 #endif
228 }
229 } // namespace Media
230 } // namespace OHOS