1 /*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifdef __ANDROID__
18 #include <dlfcn.h>
19 #include <log/log.h>
20 #include <statslog_hwui.h>
21 #include <statssocket_lazy.h>
22 #include <utils/Errors.h>
23
24 #include <mutex>
25 #endif
26
27 #include <unistd.h>
28
29 #include "StatsUtils.h"
30
31 namespace android {
32 namespace uirenderer {
33
34 #ifdef __ANDROID__
35
36 namespace {
37
toStatsColorSpaceTransfer(skcms_TFType transferType)38 int32_t toStatsColorSpaceTransfer(skcms_TFType transferType) {
39 switch (transferType) {
40 case skcms_TFType_sRGBish:
41 return stats::IMAGE_DECODED__COLOR_SPACE_TRANSFER__COLOR_SPACE_TRANSFER_SRGBISH;
42 case skcms_TFType_PQish:
43 return stats::IMAGE_DECODED__COLOR_SPACE_TRANSFER__COLOR_SPACE_TRANSFER_PQISH;
44 case skcms_TFType_HLGish:
45 return stats::IMAGE_DECODED__COLOR_SPACE_TRANSFER__COLOR_SPACE_TRANSFER_HLGISH;
46 default:
47 return stats::IMAGE_DECODED__COLOR_SPACE_TRANSFER__COLOR_SPACE_TRANSFER_UNKNOWN;
48 }
49 }
50
toStatsBitmapFormat(SkColorType type)51 int32_t toStatsBitmapFormat(SkColorType type) {
52 switch (type) {
53 case kAlpha_8_SkColorType:
54 return stats::IMAGE_DECODED__FORMAT__BITMAP_FORMAT_A_8;
55 case kRGB_565_SkColorType:
56 return stats::IMAGE_DECODED__FORMAT__BITMAP_FORMAT_RGB_565;
57 case kN32_SkColorType:
58 return stats::IMAGE_DECODED__FORMAT__BITMAP_FORMAT_ARGB_8888;
59 case kRGBA_F16_SkColorType:
60 return stats::IMAGE_DECODED__FORMAT__BITMAP_FORMAT_RGBA_F16;
61 case kRGBA_1010102_SkColorType:
62 return stats::IMAGE_DECODED__FORMAT__BITMAP_FORMAT_RGBA_1010102;
63 default:
64 return stats::IMAGE_DECODED__FORMAT__BITMAP_FORMAT_UNKNOWN;
65 }
66 }
67
68 } // namespace
69
70 #endif
71
logBitmapDecode(const SkImageInfo & info,bool hasGainmap)72 void logBitmapDecode(const SkImageInfo& info, bool hasGainmap) {
73 #ifdef __ANDROID__
74
75 if (!statssocket::lazy::IsAvailable()) {
76 std::once_flag once;
77 std::call_once(once, []() { ALOGD("libstatssocket not available, dropping stats"); });
78 return;
79 }
80
81 skcms_TFType tfnType = skcms_TFType_Invalid;
82
83 if (info.colorSpace()) {
84 skcms_TransferFunction tfn;
85 info.colorSpace()->transferFn(&tfn);
86 tfnType = skcms_TransferFunction_getType(&tfn);
87 }
88
89 auto status =
90 stats::stats_write(uirenderer::stats::IMAGE_DECODED, static_cast<int32_t>(getuid()),
91 uirenderer::toStatsColorSpaceTransfer(tfnType), hasGainmap,
92 uirenderer::toStatsBitmapFormat(info.colorType()));
93 ALOGW_IF(status != OK, "Image decoding logging dropped!");
94 #endif
95 }
96
logBitmapDecode(const Bitmap & bitmap)97 void logBitmapDecode(const Bitmap& bitmap) {
98 logBitmapDecode(bitmap.info(), bitmap.hasGainmap());
99 }
100
101 } // namespace uirenderer
102 } // namespace android
103