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 "adapter/ohos/entrance/ace_container.h"
29 #include "adapter/ohos/entrance/subwindow/subwindow_ohos.h"
30 #include "base/subwindow/subwindow_manager.h"
31 #include "base/thread/background_task_executor.h"
32 #include "base/utils/utils.h"
33 #include "base/json/json_util.h"
34 #include "base/utils/system_properties.h"
35 #include "core/common/ace_engine.h"
36 #include "core/common/connect_server_manager.h"
37 #include "core/common/container.h"
38 #include "core/common/container_scope.h"
39 #include "core/components_ng/base/inspector.h"
40 #include "core/components_v2/inspector/inspector.h"
41 #include "core/pipeline_ng/pipeline_context.h"
42 #include "dm/display_manager.h"
43 #include "foundation/ability/ability_runtime/frameworks/native/runtime/connect_server_manager.h"
44
45 namespace OHOS::Ace {
46
47 namespace {
48
ColorSpaceToSkColorSpace(const RefPtr<PixelMap> & pixmap)49 sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap)
50 {
51 return SkColorSpace::MakeSRGB();
52 }
53
AlphaTypeToSkAlphaType(const RefPtr<PixelMap> & pixmap)54 SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap)
55 {
56 switch (pixmap->GetAlphaType()) {
57 case AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
58 return SkAlphaType::kUnknown_SkAlphaType;
59 case AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
60 return SkAlphaType::kOpaque_SkAlphaType;
61 case AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
62 return SkAlphaType::kPremul_SkAlphaType;
63 case AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
64 return SkAlphaType::kUnpremul_SkAlphaType;
65 default:
66 return SkAlphaType::kUnknown_SkAlphaType;
67 }
68 }
69
PixelFormatToSkColorType(const RefPtr<PixelMap> & pixmap)70 SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap)
71 {
72 switch (pixmap->GetPixelFormat()) {
73 case PixelFormat::RGB_565:
74 return SkColorType::kRGB_565_SkColorType;
75 case PixelFormat::RGBA_8888:
76 return SkColorType::kRGBA_8888_SkColorType;
77 case PixelFormat::BGRA_8888:
78 return SkColorType::kBGRA_8888_SkColorType;
79 case PixelFormat::ALPHA_8:
80 return SkColorType::kAlpha_8_SkColorType;
81 case PixelFormat::RGBA_F16:
82 return SkColorType::kRGBA_F16_SkColorType;
83 case PixelFormat::UNKNOWN:
84 case PixelFormat::ARGB_8888:
85 case PixelFormat::RGB_888:
86 case PixelFormat::NV21:
87 case PixelFormat::NV12:
88 case PixelFormat::CMYK:
89 default:
90 return SkColorType::kUnknown_SkColorType;
91 }
92 }
93
MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap> & pixmap)94 SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap)
95 {
96 SkColorType colorType = PixelFormatToSkColorType(pixmap);
97 SkAlphaType alphaType = AlphaTypeToSkAlphaType(pixmap);
98 sk_sp<SkColorSpace> colorSpace = ColorSpaceToSkColorSpace(pixmap);
99 return SkImageInfo::Make(pixmap->GetWidth(), pixmap->GetHeight(), colorType, alphaType, colorSpace);
100 }
101
GetWindow(int32_t containerId)102 const OHOS::sptr<OHOS::Rosen::Window> GetWindow(int32_t containerId)
103 {
104 auto container = AceEngine::Get().GetContainer(containerId);
105 if (containerId >= MIN_SUBCONTAINER_ID && containerId < MIN_PLUGIN_SUBCONTAINER_ID) {
106 auto subwindow = SubwindowManager::GetInstance()->GetSubwindow(
107 SubwindowManager::GetInstance()->GetParentContainerId(containerId));
108 CHECK_NULL_RETURN(subwindow, nullptr);
109 if (AceType::InstanceOf<SubwindowOhos>(subwindow)) {
110 auto subWindowOhos = AceType::DynamicCast<SubwindowOhos>(subwindow);
111 CHECK_NULL_RETURN(subWindowOhos, nullptr);
112 return subWindowOhos->GetSubWindow();
113 }
114 } else {
115 auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
116 if (aceContainer != nullptr) {
117 return OHOS::Rosen::Window::Find(aceContainer->GetWindowName());
118 }
119 return OHOS::Rosen::Window::GetTopWindowWithId(container->GetWindowId());
120 }
121 return nullptr;
122 }
123 } // namespace
124
125 bool LayoutInspector::stateProfilerStatus_ = false;
126 bool LayoutInspector::layoutInspectorStatus_ = false;
127 std::function<void(bool)> LayoutInspector::jsStateProfilerStatusCallback_ = nullptr;
128 const char PNG_TAG[] = "png";
129
SupportInspector()130 void LayoutInspector::SupportInspector()
131 {
132 auto container = Container::Current();
133 CHECK_NULL_VOID(container);
134 if (!layoutInspectorStatus_) {
135 return;
136 }
137 std::string treeJsonStr;
138 GetInspectorTreeJsonStr(treeJsonStr, ContainerScope::CurrentId());
139 if (treeJsonStr.empty()) {
140 return;
141 }
142 auto message = JsonUtil::Create(true);
143 GetSnapshotJson(ContainerScope::CurrentId(), message);
144 CHECK_NULL_VOID(message);
145
146 auto sendTask = [treeJsonStr, jsonSnapshotStr = message->ToString(), container]() {
147 if (container->IsUseStageModel()) {
148 OHOS::AbilityRuntime::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
149 } else {
150 OHOS::Ace::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
151 }
152 };
153 BackgroundTaskExecutor::GetInstance().PostTask(std::move(sendTask));
154 }
155
SetStatus(bool layoutInspectorStatus)156 void LayoutInspector::SetStatus(bool layoutInspectorStatus)
157 {
158 layoutInspectorStatus_ = layoutInspectorStatus;
159 }
160
TriggerJsStateProfilerStatusCallback(bool status)161 void LayoutInspector::TriggerJsStateProfilerStatusCallback(bool status)
162 {
163 if (jsStateProfilerStatusCallback_) {
164 stateProfilerStatus_ = status;
165 jsStateProfilerStatusCallback_(status);
166 }
167 }
168
SetJsStateProfilerStatusCallback(ProfilerStatusCallback callback)169 void LayoutInspector::SetJsStateProfilerStatusCallback(ProfilerStatusCallback callback)
170 {
171 jsStateProfilerStatusCallback_ = callback;
172 }
173
GetStateProfilerStatus()174 bool LayoutInspector::GetStateProfilerStatus()
175 {
176 return stateProfilerStatus_;
177 }
178
SendStateProfilerMessage(const std::string & message)179 void LayoutInspector::SendStateProfilerMessage(const std::string& message)
180 {
181 OHOS::AbilityRuntime::ConnectServerManager::Get().SendArkUIStateProfilerMessage(message);
182 }
183
SetStateProfilerStatus(bool status)184 void LayoutInspector::SetStateProfilerStatus(bool status)
185 {
186 auto taskExecutor = Container::CurrentTaskExecutorSafely();
187 CHECK_NULL_VOID(taskExecutor);
188 auto task = [status]() { LayoutInspector::TriggerJsStateProfilerStatusCallback(status); };
189 taskExecutor->PostTask(std::move(task), TaskExecutor::TaskType::UI, "ArkUISetStateProfilerStatus");
190 }
191
SetCallback(int32_t instanceId)192 void LayoutInspector::SetCallback(int32_t instanceId)
193 {
194 auto container = AceEngine::Get().GetContainer(instanceId);
195 CHECK_NULL_VOID(container);
196 if (container->IsUseStageModel()) {
197 OHOS::AbilityRuntime::ConnectServerManager::Get().SetLayoutInspectorCallback(
198 [](int32_t containerId) { return CreateLayoutInfo(containerId); },
199 [](bool status) { return SetStatus(status); });
200 } else {
201 OHOS::Ace::ConnectServerManager::Get().SetLayoutInspectorCallback(
202 [](int32_t containerId) { return CreateLayoutInfo(containerId); },
203 [](bool status) { return SetStatus(status); });
204 }
205
206 OHOS::AbilityRuntime::ConnectServerManager::Get().SetStateProfilerCallback(
207 [](bool status) { return SetStateProfilerStatus(status); });
208 }
209
CreateLayoutInfo(int32_t containerId)210 void LayoutInspector::CreateLayoutInfo(int32_t containerId)
211 {
212 auto container = Container::GetFoucsed();
213 CHECK_NULL_VOID(container);
214 if (container->IsDynamicRender()) {
215 container = Container::CurrentSafely();
216 CHECK_NULL_VOID(container);
217 }
218 containerId = container->GetInstanceId();
219 ContainerScope socpe(containerId);
220 auto context = PipelineContext::GetCurrentContext();
221 CHECK_NULL_VOID(context);
222 auto getInspectorTask = [container, containerId]() {
223 std::string treeJson;
224 GetInspectorTreeJsonStr(treeJson, containerId);
225 auto message = JsonUtil::Create(true);
226 GetSnapshotJson(containerId, message);
227 CHECK_NULL_VOID(message);
228 auto sendResultTask = [treeJsonStr = std::move(treeJson), jsonSnapshotStr = message->ToString(), container]() {
229 if (container->IsUseStageModel()) {
230 OHOS::AbilityRuntime::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
231 } else {
232 OHOS::Ace::ConnectServerManager::Get().SendInspector(treeJsonStr, jsonSnapshotStr);
233 }
234 };
235 BackgroundTaskExecutor::GetInstance().PostTask(std::move(sendResultTask));
236 };
237 context->GetTaskExecutor()->PostTask(
238 std::move(getInspectorTask), TaskExecutor::TaskType::UI, "ArkUIGetInspectorTreeJson");
239 }
240
GetInspectorTreeJsonStr(std::string & treeJsonStr,int32_t containerId)241 void LayoutInspector::GetInspectorTreeJsonStr(std::string& treeJsonStr, int32_t containerId)
242 {
243 auto container = AceEngine::Get().GetContainer(containerId);
244 CHECK_NULL_VOID(container);
245 #ifdef NG_BUILD
246 treeJsonStr = NG::Inspector::GetInspector(true);
247 #else
248 if (container->IsUseNewPipeline()) {
249 if (containerId >= MIN_SUBCONTAINER_ID && containerId < MIN_PLUGIN_SUBCONTAINER_ID) {
250 treeJsonStr = NG::Inspector::GetSubWindowInspector(true);
251 } else {
252 treeJsonStr = NG::Inspector::GetInspector(true);
253 }
254 } else {
255 auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
256 CHECK_NULL_VOID(pipelineContext);
257 treeJsonStr = V2::Inspector::GetInspectorTree(pipelineContext, true);
258 }
259 #endif
260 }
261
GetSnapshotJson(int32_t containerId,std::unique_ptr<JsonValue> & message)262 void LayoutInspector::GetSnapshotJson(int32_t containerId, std::unique_ptr<JsonValue>& message)
263 {
264 auto container = AceEngine::Get().GetContainer(containerId);
265 CHECK_NULL_VOID(container);
266 OHOS::sptr<OHOS::Rosen::Window> window = GetWindow(containerId);
267 CHECK_NULL_VOID(window);
268 auto pixelMap = window->Snapshot();
269 CHECK_NULL_VOID(pixelMap);
270 auto acePixelMap = AceType::MakeRefPtr<PixelMapOhos>(pixelMap);
271 CHECK_NULL_VOID(acePixelMap);
272 auto imageInfo = MakeSkImageInfoFromPixelMap(acePixelMap);
273 SkPixmap imagePixmap(
274 imageInfo, reinterpret_cast<const void*>(acePixelMap->GetPixels()), acePixelMap->GetRowBytes());
275 sk_sp<SkImage> image;
276 image = SkImage::MakeFromRaster(imagePixmap, &PixelMap::ReleaseProc, PixelMap::GetReleaseContext(acePixelMap));
277 CHECK_NULL_VOID(image);
278 auto data = image->encodeToData(SkEncodedImageFormat::kPNG, 100);
279 CHECK_NULL_VOID(data);
280 auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
281 CHECK_NULL_VOID(defaultDisplay);
282 auto deviceDpi = defaultDisplay->GetDpi();
283 auto deviceWidth = defaultDisplay->GetWidth();
284 auto deviceHeight = defaultDisplay->GetHeight();
285 message->Put("type", "snapShot");
286 message->Put("format", PNG_TAG);
287 message->Put("width", (*pixelMap).GetWidth());
288 message->Put("height", (*pixelMap).GetHeight());
289 message->Put("posX", container->GetViewPosX());
290 message->Put("posY", container->GetViewPosY());
291 message->Put("deviceWidth", deviceWidth);
292 message->Put("deviceHeight", deviceHeight);
293 message->Put("deviceDpi", deviceDpi);
294 int32_t encodeLength = static_cast<int32_t>(SkBase64::Encode(data->data(), data->size(), nullptr));
295 message->Put("size", data->size());
296 SkString info(encodeLength);
297 SkBase64::Encode(data->data(), data->size(), info.writable_str());
298 message->Put("pixelMapBase64", info.c_str());
299 }
300
301 } // namespace OHOS::Ace
302