1 /*
2 * Copyright (c) 2021-2023 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 "frameworks/bridge/declarative_frontend/jsview/js_image.h"
17
18 #if !defined(PREVIEW)
19 #include <dlfcn.h>
20 #endif
21
22 #include "base/image/pixel_map.h"
23 #include "base/log/ace_scoring_log.h"
24 #include "base/log/ace_trace.h"
25 #include "bridge/declarative_frontend/engine/functions/js_drag_function.h"
26 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
27 #include "bridge/declarative_frontend/jsview/models/image_model_impl.h"
28 #include "core/common/container.h"
29 #include "core/components/image/image_event.h"
30 #include "core/components_ng/event/gesture_event_hub.h"
31 #include "core/components_ng/pattern/image/image_model.h"
32 #include "core/components_ng/pattern/image/image_model_ng.h"
33 #include "core/image/image_source_info.h"
34
35 namespace OHOS::Ace {
36
37 std::unique_ptr<ImageModel> ImageModel::instance_ = nullptr;
38 std::mutex ImageModel::mutex_;
39
GetInstance()40 ImageModel* ImageModel::GetInstance()
41 {
42 if (!instance_) {
43 std::lock_guard<std::mutex> lock(mutex_);
44 if (!instance_) {
45 #ifdef NG_BUILD
46 instance_.reset(new NG::ImageModelNG());
47 #else
48 if (Container::IsCurrentUseNewPipeline()) {
49 instance_.reset(new NG::ImageModelNG());
50 } else {
51 instance_.reset(new Framework::ImageModelImpl());
52 }
53 #endif
54 }
55 }
56 return instance_.get();
57 }
58
59 } // namespace OHOS::Ace
60
61 namespace OHOS::Ace::Framework {
62
LoadImageSuccEventToJSValue(const LoadImageSuccessEvent & eventInfo)63 JSRef<JSVal> LoadImageSuccEventToJSValue(const LoadImageSuccessEvent& eventInfo)
64 {
65 JSRef<JSObject> obj = JSRef<JSObject>::New();
66 obj->SetProperty("width", eventInfo.GetWidth());
67 obj->SetProperty("height", eventInfo.GetHeight());
68 obj->SetProperty("componentWidth", eventInfo.GetComponentWidth());
69 obj->SetProperty("componentHeight", eventInfo.GetComponentHeight());
70 obj->SetProperty("loadingStatus", eventInfo.GetLoadingStatus());
71 obj->SetProperty("contentWidth", eventInfo.GetContentWidth());
72 obj->SetProperty("contentHeight", eventInfo.GetContentHeight());
73 obj->SetProperty("contentOffsetX", eventInfo.GetContentOffsetX());
74 obj->SetProperty("contentOffsetY", eventInfo.GetContentOffsetY());
75 return JSRef<JSVal>::Cast(obj);
76 }
77
LoadImageFailEventToJSValue(const LoadImageFailEvent & eventInfo)78 JSRef<JSVal> LoadImageFailEventToJSValue(const LoadImageFailEvent& eventInfo)
79 {
80 JSRef<JSObject> obj = JSRef<JSObject>::New();
81 obj->SetProperty("componentWidth", eventInfo.GetComponentWidth());
82 obj->SetProperty("componentHeight", eventInfo.GetComponentHeight());
83 obj->SetProperty("message", eventInfo.GetErrorMessage());
84 return JSRef<JSVal>::Cast(obj);
85 }
86
SetAlt(const JSCallbackInfo & args)87 void JSImage::SetAlt(const JSCallbackInfo& args)
88 {
89 if (args.Length() < 1) {
90 LOGE("The argv is wrong, it it supposed to have at least 1 argument");
91 return;
92 }
93
94 std::string src;
95 if (!ParseJsMedia(args[0], src)) {
96 return;
97 }
98 if (ImageSourceInfo::ResolveURIType(src) == SrcType::NETWORK) {
99 LOGW("Alt doesn't support network image %{public}s", src.c_str());
100 return;
101 }
102 ImageModel::GetInstance()->SetAlt(src);
103 }
104
SetObjectFit(int32_t value)105 void JSImage::SetObjectFit(int32_t value)
106 {
107 auto fit = static_cast<ImageFit>(value);
108 if (fit < ImageFit::FILL || fit > ImageFit::SCALE_DOWN) {
109 LOGW("The value of objectFit is out of range %{public}d", value);
110 fit = ImageFit::COVER;
111 }
112 ImageModel::GetInstance()->SetImageFit(fit);
113 }
114
SetMatchTextDirection(bool value)115 void JSImage::SetMatchTextDirection(bool value)
116 {
117 ImageModel::GetInstance()->SetMatchTextDirection(value);
118 }
119
SetFitOriginalSize(bool value)120 void JSImage::SetFitOriginalSize(bool value)
121 {
122 ImageModel::GetInstance()->SetFitOriginSize(value);
123 }
124
SetBorder(const Border & border)125 void JSImage::SetBorder(const Border& border)
126 {
127 ImageModel::GetInstance()->SetBorder(border);
128 }
129
OnComplete(const JSCallbackInfo & args)130 void JSImage::OnComplete(const JSCallbackInfo& args)
131 {
132 LOGD("JSImage OnComplete");
133 if (args[0]->IsFunction()) {
134 auto jsLoadSuccFunc = AceType::MakeRefPtr<JsEventFunction<LoadImageSuccessEvent, 1>>(
135 JSRef<JSFunc>::Cast(args[0]), LoadImageSuccEventToJSValue);
136
137 auto onComplete = [execCtx = args.GetExecutionContext(), func = std::move(jsLoadSuccFunc)](
138 const LoadImageSuccessEvent& info) {
139 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
140 ACE_SCORING_EVENT("Image.onComplete");
141 func->Execute(info);
142 };
143 ImageModel::GetInstance()->SetOnComplete(std::move(onComplete));
144 } else {
145 LOGE("args not function");
146 }
147 }
148
OnError(const JSCallbackInfo & args)149 void JSImage::OnError(const JSCallbackInfo& args)
150 {
151 LOGD("JSImage OnError");
152 if (args[0]->IsFunction()) {
153 auto jsLoadFailFunc = AceType::MakeRefPtr<JsEventFunction<LoadImageFailEvent, 1>>(
154 JSRef<JSFunc>::Cast(args[0]), LoadImageFailEventToJSValue);
155 auto onError = [execCtx = args.GetExecutionContext(), func = std::move(jsLoadFailFunc)](
156 const LoadImageFailEvent& info) {
157 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
158 ACE_SCORING_EVENT("Image.onError");
159 func->Execute(info);
160 };
161
162 ImageModel::GetInstance()->SetOnError(onError);
163 } else {
164 LOGE("args not function");
165 }
166 }
167
OnFinish(const JSCallbackInfo & info)168 void JSImage::OnFinish(const JSCallbackInfo& info)
169 {
170 auto tmpInfo = info[0];
171 if (!tmpInfo->IsFunction()) {
172 return;
173 }
174 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(tmpInfo));
175 auto onFinish = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)]() {
176 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
177 ACE_SCORING_EVENT("Image.onFinish");
178 func->Execute();
179 };
180 ImageModel::GetInstance()->SetSvgAnimatorFinishEvent(onFinish);
181 }
182
Create(const JSCallbackInfo & info)183 void JSImage::Create(const JSCallbackInfo& info)
184 {
185 if (info.Length() < 1) {
186 return;
187 }
188
189 auto context = PipelineBase::GetCurrentContext();
190 CHECK_NULL_VOID(context);
191 bool isCard = context->IsFormRender();
192
193 // Interim programme
194 std::string bundleName;
195 std::string moduleName;
196 std::string src;
197 bool srcValid = ParseJsMedia(info[0], src);
198 if (isCard && info[0]->IsString()) {
199 SrcType srcType = ImageSourceInfo::ResolveURIType(src);
200 bool notSupport = (srcType == SrcType::NETWORK || srcType == SrcType::FILE || srcType == SrcType::DATA_ABILITY);
201 if (notSupport) {
202 src.clear();
203 }
204 }
205 GetJsMediaBundleInfo(info[0], bundleName, moduleName);
206 RefPtr<PixelMap> pixmap = nullptr;
207
208 // input is PixelMap / Drawable
209 if (!srcValid) {
210 #if defined(PIXEL_MAP_SUPPORTED)
211 if (isCard) {
212 LOGE("Not supported pixmap when form render");
213 } else {
214 if (IsDrawable(info[0])) {
215 pixmap = GetDrawablePixmap(info[0]);
216 } else {
217 pixmap = CreatePixelMapFromNapiValue(info[0]);
218 }
219 }
220 #else
221 LOGW("Pixmap not supported under this environment.");
222 #endif
223 }
224
225 ImageModel::GetInstance()->Create(src, pixmap, bundleName, moduleName);
226 }
227
IsDrawable(const JSRef<JSVal> & jsValue)228 bool JSImage::IsDrawable(const JSRef<JSVal>& jsValue)
229 {
230 if (!jsValue->IsObject()) {
231 return false;
232 }
233 JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(jsValue);
234 if (jsObj->IsUndefined()) {
235 return false;
236 }
237
238 // if jsObject has function getPixelMap, it's a DrawableDescriptor object
239 JSRef<JSVal> func = jsObj->GetProperty("getPixelMap");
240 return (!func->IsNull() && func->IsFunction());
241 }
242
JsBorder(const JSCallbackInfo & info)243 void JSImage::JsBorder(const JSCallbackInfo& info)
244 {
245 JSViewAbstract::JsBorder(info);
246 ImageModel::GetInstance()->SetBackBorder();
247 }
248
JsBorderRadius(const JSCallbackInfo & info)249 void JSImage::JsBorderRadius(const JSCallbackInfo& info)
250 {
251 JSViewAbstract::JsBorderRadius(info);
252 ImageModel::GetInstance()->SetBackBorder();
253 }
254
SetSourceSize(const JSCallbackInfo & info)255 void JSImage::SetSourceSize(const JSCallbackInfo& info)
256 {
257 ImageModel::GetInstance()->SetImageSourceSize(JSViewAbstract::ParseSize(info));
258 }
259
SetImageFill(const JSCallbackInfo & info)260 void JSImage::SetImageFill(const JSCallbackInfo& info)
261 {
262 if (info.Length() < 1) {
263 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
264 return;
265 }
266
267 Color color;
268 if (!ParseJsColor(info[0], color)) {
269 return;
270 }
271 ImageModel::GetInstance()->SetImageFill(color);
272 }
273
SetImageRenderMode(int32_t imageRenderMode)274 void JSImage::SetImageRenderMode(int32_t imageRenderMode)
275 {
276 auto renderMode = static_cast<ImageRenderMode>(imageRenderMode);
277 if (renderMode < ImageRenderMode::ORIGINAL || renderMode > ImageRenderMode::TEMPLATE) {
278 LOGW("invalid imageRenderMode value %{public}d", imageRenderMode);
279 renderMode = ImageRenderMode::ORIGINAL;
280 }
281 ImageModel::GetInstance()->SetImageRenderMode(renderMode);
282 }
283
SetImageInterpolation(int32_t imageInterpolation)284 void JSImage::SetImageInterpolation(int32_t imageInterpolation)
285 {
286 auto interpolation = static_cast<ImageInterpolation>(imageInterpolation);
287 if (interpolation < ImageInterpolation::NONE || interpolation > ImageInterpolation::HIGH) {
288 LOGW("invalid imageInterpolation value %{public}d", imageInterpolation);
289 interpolation = ImageInterpolation::NONE;
290 }
291 ImageModel::GetInstance()->SetImageInterpolation(interpolation);
292 }
293
SetImageRepeat(int32_t imageRepeat)294 void JSImage::SetImageRepeat(int32_t imageRepeat)
295 {
296 auto repeat = static_cast<ImageRepeat>(imageRepeat);
297 if (repeat < ImageRepeat::NO_REPEAT || repeat > ImageRepeat::REPEAT) {
298 LOGW("invalid imageRepeat value %{public}d", imageRepeat);
299 repeat = ImageRepeat::NO_REPEAT;
300 }
301 ImageModel::GetInstance()->SetImageRepeat(repeat);
302 }
303
JsTransition(const JSCallbackInfo & info)304 void JSImage::JsTransition(const JSCallbackInfo& info)
305 {
306 if (ImageModel::GetInstance()->IsSrcSvgImage()) {
307 JSViewAbstract::JsTransition(info);
308 } else {
309 JSViewAbstract::JsTransitionPassThrough(info);
310 }
311 }
312
JsOpacity(const JSCallbackInfo & info)313 void JSImage::JsOpacity(const JSCallbackInfo& info)
314 {
315 if (ImageModel::GetInstance()->IsSrcSvgImage()) {
316 JSViewAbstract::JsOpacity(info);
317 } else {
318 JSViewAbstract::JsOpacityPassThrough(info);
319 }
320 }
321
JsBlur(const JSCallbackInfo & info)322 void JSImage::JsBlur(const JSCallbackInfo& info)
323 {
324 // only flutter runs special image blur
325 #ifdef ENABLE_ROSEN_BACKEND
326 JSViewAbstract::JsBlur(info);
327 #else
328 if (info.Length() < 1) {
329 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
330 return;
331 }
332 double blur = 0.0;
333 if (ParseJsDouble(info[0], blur)) {
334 ImageModel::GetInstance()->SetBlur(blur);
335 }
336 #endif
337 }
338
SetAutoResize(bool autoResize)339 void JSImage::SetAutoResize(bool autoResize)
340 {
341 ImageModel::GetInstance()->SetAutoResize(autoResize);
342 }
343
SetSyncLoad(const JSCallbackInfo & info)344 void JSImage::SetSyncLoad(const JSCallbackInfo& info)
345 {
346 if (info.Length() < 1) {
347 return;
348 }
349 auto tmpInfo = info[0];
350 if (!tmpInfo->IsBoolean()) {
351 return;
352 }
353 ImageModel::GetInstance()->SetSyncMode(tmpInfo->ToBoolean());
354 }
355
ConstructorCallback(const JSCallbackInfo & args)356 void JSColorFilter::ConstructorCallback(const JSCallbackInfo& args)
357 {
358 if (args.Length() < 1) {
359 return;
360 }
361 auto tmpInfo = args[0];
362 if (!tmpInfo->IsArray()) {
363 LOGD("jscallback is not object or array");
364 return;
365 }
366 JSRef<JSArray> array = JSRef<JSArray>::Cast(tmpInfo);
367 if (array->Length() != COLOR_FILTER_MATRIX_SIZE) {
368 LOGI("arg length illegal");
369 return;
370 }
371 auto jscolorfilter = Referenced::MakeRefPtr<JSColorFilter>();
372 if (jscolorfilter == nullptr) {
373 LOGW("make jscolorfilter object failed");
374 return;
375 }
376 std::vector<float> colorfilter;
377 for (size_t i = 0; i < array->Length(); i++) {
378 JSRef<JSVal> value = array->GetValueAt(i);
379 if (value->IsNumber()) {
380 colorfilter.emplace_back(value->ToNumber<float>());
381 }
382 }
383 if (colorfilter.size() != COLOR_FILTER_MATRIX_SIZE) {
384 LOGE("colorfilter length illegal");
385 return;
386 }
387 jscolorfilter->SetColorFilterMatrix(std::move(colorfilter));
388 jscolorfilter->IncRefCount();
389 args.SetReturnValue(Referenced::RawPtr(jscolorfilter));
390 }
391
DestructorCallback(JSColorFilter * obj)392 void JSColorFilter::DestructorCallback(JSColorFilter* obj)
393 {
394 if (obj != nullptr) {
395 obj->DecRefCount();
396 }
397 }
398
SetColorFilter(const JSCallbackInfo & info)399 void JSImage::SetColorFilter(const JSCallbackInfo& info)
400 {
401 if (info.Length() != 1) {
402 return;
403 }
404 auto tmpInfo = info[0];
405 if (!tmpInfo->IsArray()) {
406 return;
407 }
408 JSRef<JSArray> array = JSRef<JSArray>::Cast(tmpInfo);
409 if (array->Length() != COLOR_FILTER_MATRIX_SIZE) {
410 LOGW("arg length illegal");
411 return;
412 }
413 std::vector<float> colorfilter;
414 for (size_t i = 0; i < array->Length(); i++) {
415 JSRef<JSVal> value = array->GetValueAt(i);
416 if (value->IsNumber()) {
417 colorfilter.emplace_back(value->ToNumber<float>());
418 }
419 }
420 if (colorfilter.size() != COLOR_FILTER_MATRIX_SIZE) {
421 LOGI("colorfilter length illegal");
422 return;
423 }
424 ImageModel::GetInstance()->SetColorFilterMatrix(colorfilter);
425 }
426
JSBind(BindingTarget globalObj)427 void JSImage::JSBind(BindingTarget globalObj)
428 {
429 JSClass<JSImage>::Declare("Image");
430 MethodOptions opt = MethodOptions::NONE;
431 JSClass<JSImage>::StaticMethod("create", &JSImage::Create, opt);
432 JSClass<JSImage>::StaticMethod("alt", &JSImage::SetAlt, opt);
433 JSClass<JSImage>::StaticMethod("objectFit", &JSImage::SetObjectFit, opt);
434 JSClass<JSImage>::StaticMethod("matchTextDirection", &JSImage::SetMatchTextDirection, opt);
435 JSClass<JSImage>::StaticMethod("fitOriginalSize", &JSImage::SetFitOriginalSize, opt);
436 JSClass<JSImage>::StaticMethod("sourceSize", &JSImage::SetSourceSize, opt);
437 JSClass<JSImage>::StaticMethod("fillColor", &JSImage::SetImageFill, opt);
438 JSClass<JSImage>::StaticMethod("renderMode", &JSImage::SetImageRenderMode, opt);
439 JSClass<JSImage>::StaticMethod("objectRepeat", &JSImage::SetImageRepeat, opt);
440 JSClass<JSImage>::StaticMethod("interpolation", &JSImage::SetImageInterpolation, opt);
441 JSClass<JSImage>::StaticMethod("colorFilter", &JSImage::SetColorFilter, opt);
442
443 JSClass<JSImage>::StaticMethod("border", &JSImage::JsBorder);
444 JSClass<JSImage>::StaticMethod("borderRadius", &JSImage::JsBorderRadius);
445 JSClass<JSImage>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
446 JSClass<JSImage>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
447 JSClass<JSImage>::StaticMethod("autoResize", &JSImage::SetAutoResize);
448
449 JSClass<JSImage>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
450 JSClass<JSImage>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
451 JSClass<JSImage>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
452 JSClass<JSImage>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
453 JSClass<JSImage>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
454 JSClass<JSImage>::StaticMethod("onComplete", &JSImage::OnComplete);
455 JSClass<JSImage>::StaticMethod("onError", &JSImage::OnError);
456 JSClass<JSImage>::StaticMethod("onFinish", &JSImage::OnFinish);
457 JSClass<JSImage>::StaticMethod("syncLoad", &JSImage::SetSyncLoad);
458 JSClass<JSImage>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
459 JSClass<JSImage>::StaticMethod("draggable", &JSImage::JsSetDraggable);
460 JSClass<JSImage>::StaticMethod("onDragStart", &JSImage::JsOnDragStart);
461 JSClass<JSImage>::StaticMethod("copyOption", &JSImage::SetCopyOption);
462 // override method
463 JSClass<JSImage>::StaticMethod("opacity", &JSImage::JsOpacity);
464 JSClass<JSImage>::StaticMethod("blur", &JSImage::JsBlur);
465 JSClass<JSImage>::StaticMethod("transition", &JSImage::JsTransition);
466 JSClass<JSImage>::InheritAndBind<JSViewAbstract>(globalObj);
467
468 JSClass<JSColorFilter>::Declare("ColorFilter");
469 JSClass<JSColorFilter>::Bind(globalObj, JSColorFilter::ConstructorCallback, JSColorFilter::DestructorCallback);
470 }
471
JsSetDraggable(bool draggable)472 void JSImage::JsSetDraggable(bool draggable)
473 {
474 ImageModel::GetInstance()->SetDraggable(draggable);
475 }
476
JsOnDragStart(const JSCallbackInfo & info)477 void JSImage::JsOnDragStart(const JSCallbackInfo& info)
478 {
479 if (info.Length() != 1 || !info[0]->IsFunction()) {
480 return;
481 }
482 RefPtr<JsDragFunction> jsOnDragStartFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
483 auto onDragStartId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragStartFunc)](
484 const RefPtr<DragEvent>& info, const std::string& extraParams) -> NG::DragDropBaseInfo {
485 NG::DragDropBaseInfo itemInfo;
486 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, itemInfo);
487
488 auto ret = func->Execute(info, extraParams);
489 if (!ret->IsObject()) {
490 return itemInfo;
491 }
492 if (ParseAndUpdateDragItemInfo(ret, itemInfo)) {
493 return itemInfo;
494 }
495
496 auto builderObj = JSRef<JSObject>::Cast(ret);
497 #if defined(PIXEL_MAP_SUPPORTED)
498 auto pixmap = builderObj->GetProperty("pixelMap");
499 itemInfo.pixelMap = CreatePixelMapFromNapiValue(pixmap);
500 #endif
501 auto extraInfo = builderObj->GetProperty("extraInfo");
502 ParseJsString(extraInfo, itemInfo.extraInfo);
503 ParseAndUpdateDragItemInfo(builderObj->GetProperty("builder"), itemInfo);
504 return itemInfo;
505 };
506 ImageModel::GetInstance()->SetOnDragStart(std::move(onDragStartId));
507 }
508
SetCopyOption(const JSCallbackInfo & info)509 void JSImage::SetCopyOption(const JSCallbackInfo& info)
510 {
511 auto copyOptions = CopyOptions::None;
512 if (info[0]->IsNumber()) {
513 auto enumNumber = info[0]->ToNumber<int>();
514 copyOptions = static_cast<CopyOptions>(enumNumber);
515 if (copyOptions < CopyOptions::None || copyOptions > CopyOptions::Distributed) {
516 copyOptions = CopyOptions::None;
517 }
518 }
519 ImageModel::GetInstance()->SetCopyOption(copyOptions);
520 }
521
522 } // namespace OHOS::Ace::Framework
523