1 /*
2 * Copyright (c) 2022 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 "core/common/layout_inspector.h"
17
18 #include <string>
19
20 #include "include/core/SkImage.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkColorSpace.h"
23 #include "include/utils/SkBase64.h"
24
25 #include "wm/window.h"
26
27 #include "adapter/ohos/osal/pixel_map_ohos.h"
28 #include "base/thread/background_task_executor.h"
29 #include "base/utils/utils.h"
30 #include "core/common/ace_engine.h"
31 #include "core/common/connect_server_manager.h"
32 #include "core/common/container.h"
33 #include "core/common/container_scope.h"
34 #include "core/components_ng/base/inspector.h"
35 #include "core/components_v2/inspector/inspector.h"
36 #include "dm/display_manager.h"
37 #include "foundation/ability/ability_runtime/frameworks/native/runtime/connect_server_manager.h"
38
39 namespace OHOS::Ace {
40
41 namespace {
42
ColorSpaceToSkColorSpace(const RefPtr<PixelMap> & pixmap)43 sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap)
44 {
45 return SkColorSpace::MakeSRGB();
46 }
47
AlphaTypeToSkAlphaType(const RefPtr<PixelMap> & pixmap)48 SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap)
49 {
50 switch (pixmap->GetAlphaType()) {
51 case AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
52 return SkAlphaType::kUnknown_SkAlphaType;
53 case AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
54 return SkAlphaType::kOpaque_SkAlphaType;
55 case AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
56 return SkAlphaType::kPremul_SkAlphaType;
57 case AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
58 return SkAlphaType::kUnpremul_SkAlphaType;
59 default:
60 return SkAlphaType::kUnknown_SkAlphaType;
61 }
62 }
63
PixelFormatToSkColorType(const RefPtr<PixelMap> & pixmap)64 SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap)
65 {
66 switch (pixmap->GetPixelFormat()) {
67 case PixelFormat::RGB_565:
68 return SkColorType::kRGB_565_SkColorType;
69 case PixelFormat::RGBA_8888:
70 return SkColorType::kRGBA_8888_SkColorType;
71 case PixelFormat::BGRA_8888:
72 return SkColorType::kBGRA_8888_SkColorType;
73 case PixelFormat::ALPHA_8:
74 return SkColorType::kAlpha_8_SkColorType;
75 case PixelFormat::RGBA_F16:
76 return SkColorType::kRGBA_F16_SkColorType;
77 case PixelFormat::UNKNOWN:
78 case PixelFormat::ARGB_8888:
79 case PixelFormat::RGB_888:
80 case PixelFormat::NV21:
81 case PixelFormat::NV12:
82 case PixelFormat::CMYK:
83 default:
84 return SkColorType::kUnknown_SkColorType;
85 }
86 }
87
MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap> & pixmap)88 SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap)
89 {
90 SkColorType colorType = PixelFormatToSkColorType(pixmap);
91 SkAlphaType alphaType = AlphaTypeToSkAlphaType(pixmap);
92 sk_sp<SkColorSpace> colorSpace = ColorSpaceToSkColorSpace(pixmap);
93 return SkImageInfo::Make(pixmap->GetWidth(), pixmap->GetHeight(), colorType, alphaType, colorSpace);
94 }
95 } // namespace
96
97 bool LayoutInspector::layoutInspectorStatus_ = false;
98 const char PNG_TAG[] = "png";
99
SupportInspector()100 void LayoutInspector::SupportInspector()
101 {
102 auto container = Container::Current();
103 CHECK_NULL_VOID_NOLOG(container);
104 if (!layoutInspectorStatus_) {
105 return;
106 }
107 std::string treeJsonStr;
108 GetInspectorTreeJsonStr(treeJsonStr, ContainerScope::CurrentId());
109 if (treeJsonStr.empty()) {
110 return;
111 }
112 auto message = JsonUtil::Create(true);
113 GetSnapshotJson(ContainerScope::CurrentId(), message);
114 CHECK_NULL_VOID(message);
115
116 auto sendTask = [treeJsonStr, jsonSnapshotStr = message->ToString(), container]() {
117 if (container->IsUseStageModel()) {
118 OHOS::AbilityRuntime::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
119 } else {
120 OHOS::Ace::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
121 }
122 };
123 BackgroundTaskExecutor::GetInstance().PostTask(std::move(sendTask));
124 }
125
SetStatus(bool layoutInspectorStatus)126 void LayoutInspector::SetStatus(bool layoutInspectorStatus)
127 {
128 layoutInspectorStatus_ = layoutInspectorStatus;
129 }
130
SetCallback(int32_t instanceId)131 void LayoutInspector::SetCallback(int32_t instanceId)
132 {
133 LOGI("SetCallback start");
134 auto container = AceEngine::Get().GetContainer(instanceId);
135 CHECK_NULL_VOID_NOLOG(container);
136 if (container->IsUseStageModel()) {
137 OHOS::AbilityRuntime::ConnectServerManager::Get().SetLayoutInspectorCallback(
138 [](int32_t containerId) { return CreateLayoutInfo(containerId); },
139 [](bool status) { return SetStatus(status); });
140 } else {
141 OHOS::Ace::ConnectServerManager::Get().SetLayoutInspectorCallback(
142 [](int32_t containerId) { return CreateLayoutInfo(containerId); },
143 [](bool status) { return SetStatus(status); });
144 }
145 }
146
CreateLayoutInfo(int32_t containerId)147 void LayoutInspector::CreateLayoutInfo(int32_t containerId)
148 {
149 LOGI("CreateLayoutInfo start");
150 ContainerScope sope(containerId);
151 auto container = AceEngine::Get().GetContainer(containerId);
152 CHECK_NULL_VOID_NOLOG(container);
153 std::string treeJsonStr;
154 GetInspectorTreeJsonStr(treeJsonStr, containerId);
155 auto message = JsonUtil::Create(true);
156 GetSnapshotJson(containerId, message);
157 CHECK_NULL_VOID(message);
158
159 auto sendTask = [treeJsonStr, jsonSnapshotStr = message->ToString(), container]() {
160 if (container->IsUseStageModel()) {
161 OHOS::AbilityRuntime::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
162 } else {
163 OHOS::Ace::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
164 }
165 };
166 BackgroundTaskExecutor::GetInstance().PostTask(std::move(sendTask));
167 }
168
GetInspectorTreeJsonStr(std::string & treeJsonStr,int32_t containerId)169 void LayoutInspector::GetInspectorTreeJsonStr(std::string& treeJsonStr, int32_t containerId)
170 {
171 auto container = AceEngine::Get().GetContainer(containerId);
172 CHECK_NULL_VOID_NOLOG(container);
173 if (container->IsUseNewPipeline()) {
174 treeJsonStr = NG::Inspector::GetInspector(true);
175 } else {
176 auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
177 CHECK_NULL_VOID(pipelineContext);
178 treeJsonStr = V2::Inspector::GetInspectorTree(pipelineContext, true);
179 }
180 }
181
GetSnapshotJson(int32_t containerId,std::unique_ptr<JsonValue> & message)182 void LayoutInspector::GetSnapshotJson(int32_t containerId, std::unique_ptr<JsonValue>& message)
183 {
184 LOGI("GetSnapshotJson start");
185 auto container = AceEngine::Get().GetContainer(containerId);
186 CHECK_NULL_VOID_NOLOG(container);
187
188 OHOS::sptr<OHOS::Rosen::Window> window = OHOS::Rosen::Window::GetTopWindowWithId(container->GetWindowId());
189 CHECK_NULL_VOID_NOLOG(window);
190 auto pixelMap = window->Snapshot();
191 CHECK_NULL_VOID(pixelMap);
192 auto acePixelMap = AceType::MakeRefPtr<PixelMapOhos>(pixelMap);
193 CHECK_NULL_VOID(acePixelMap);
194 auto imageInfo = MakeSkImageInfoFromPixelMap(acePixelMap);
195 SkPixmap imagePixmap(
196 imageInfo, reinterpret_cast<const void*>(acePixelMap->GetPixels()), acePixelMap->GetRowBytes());
197 sk_sp<SkImage> image;
198 image = SkImage::MakeFromRaster(imagePixmap, &PixelMap::ReleaseProc, PixelMap::GetReleaseContext(acePixelMap));
199 CHECK_NULL_VOID(image);
200 auto data = image->encodeToData(SkEncodedImageFormat::kPNG, 100);
201 CHECK_NULL_VOID(data);
202 auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
203 CHECK_NULL_VOID(defaultDisplay);
204 auto deviceDpi = defaultDisplay->GetDpi();
205 auto deviceWidth = defaultDisplay->GetWidth();
206 auto deviceHeight = defaultDisplay->GetHeight();
207 LOGI("GetSnapshotJson: deviceWidth: %{public}d, deviceHeight: %{public}d, dpi: %{public}d", deviceWidth,
208 deviceHeight, deviceDpi);
209 message->Put("type", "snapShot");
210 message->Put("format", PNG_TAG);
211 message->Put("width", (*pixelMap).GetWidth());
212 message->Put("height", (*pixelMap).GetHeight());
213 message->Put("posX", container->GetViewPosX());
214 message->Put("posY", container->GetViewPosY());
215 message->Put("deviceWidth", deviceWidth);
216 message->Put("deviceHeight", deviceHeight);
217 message->Put("deviceDpi", deviceDpi);
218 int32_t encodeLength = SkBase64::Encode(data->data(), data->size(), nullptr);
219 message->Put("size", data->size());
220 SkString info(encodeLength);
221 SkBase64::Encode(data->data(), data->size(), info.writable_str());
222 message->Put("pixelMapBase64", info.c_str());
223 LOGI("GetSnapshotJson pixelMapBase64:%{public}s", info.c_str());
224 }
225
226 } // namespace OHOS::Ace