• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RENDER_IMAGE_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RENDER_IMAGE_H
18 
19 #include "base/geometry/ng/size_t.h"
20 #include "base/memory/referenced.h"
21 #include "bridge/declarative_frontend/engine/bindings_defines.h"
22 #include "core/components_ng/image_provider/svg_dom_base.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
24 #include "frameworks/core/components_ng/image_provider/image_loading_context.h"
25 
26 namespace OHOS::Ace::Framework {
27 class JSCanvasRenderer;
28 
29 void BindNativeFunction(napi_env env, napi_value object, const char* name, napi_callback func);
30 void* GetNapiCallbackInfoAndThis(napi_env env, napi_callback_info info);
31 
32 class JSRenderImage : public Referenced {
33 public:
34     JSRenderImage();
35     ~JSRenderImage() override = default;
36 
37     static void JSBind(BindingTarget globalObj, void* nativeEngine = nullptr);
38 
39     static napi_value InitImageBitmap(napi_env env);
40     static napi_value Constructor(napi_env env, napi_callback_info info);
41     static napi_value JsClose(napi_env env, napi_callback_info info);
42     static napi_value JsSetWidth(napi_env env, napi_callback_info info);
43     static napi_value JsSetHeight(napi_env env, napi_callback_info info);
44     static napi_value JsGetWidth(napi_env env, napi_callback_info info);
45     static napi_value JsGetHeight(napi_env env, napi_callback_info info);
46 
47     double GetWidth();
48     double GetHeight();
49     std::string GetSrc();
50     void SetCloseCallback(std::function<void()>&& callback);
GetPixelMap()51     RefPtr<PixelMap> GetPixelMap()
52     {
53         return pixelMap_;
54     }
55 
GetSvgDom()56     RefPtr<NG::SvgDomBase> GetSvgDom()
57     {
58         return svgDom_;
59     }
60 
SetInstanceId(int32_t instanceId)61     void SetInstanceId(int32_t instanceId)
62     {
63         instanceId_ = instanceId;
64     }
65 
GetInstanceId()66     int32_t GetInstanceId()
67     {
68         return instanceId_;
69     }
70 
IsSvg()71     bool IsSvg()
72     {
73         return sourceInfo_.IsSvg();
74     }
75 
GetImageFit()76     ImageFit GetImageFit()
77     {
78         return imageFit_;
79     }
80 
GetImageSize()81     NG::SizeF GetImageSize()
82     {
83         return imageSize_;
84     }
85 
86     ACE_DISALLOW_COPY_AND_MOVE(JSRenderImage);
87 private:
88     napi_value OnClose();
89     napi_value OnGetWidth(napi_env env);
90     napi_value OnGetHeight(napi_env env);
91     napi_value OnSetWidth();
92     napi_value OnSetHeight();
93 
94     void LoadImage(const std::string& src);
95     void LoadImage(const ImageSourceInfo& src);
96     void OnImageDataReady();
97     void OnImageLoadFail(const std::string& errorMsg);
98     void OnImageLoadSuccess();
99 
100     RefPtr<NG::CanvasImage> image_;
101     RefPtr<NG::ImageObject> imageObj_;
102     RefPtr<NG::ImageLoadingContext> loadingCtx_;
103     RefPtr<PixelMap> pixelMap_;
104     RefPtr<NG::SvgDomBase> svgDom_;
105     ImageSourceInfo sourceInfo_;
106     ImageFit imageFit_ = ImageFit::NONE;
107     NG::SizeF imageSize_;
108 
109     std::string src_;
110     std::list<std::function<void()>> closeCallbacks_;
111     double width_ = 0;
112     double height_ = 0;
113     int32_t instanceId_ = 0;
114 };
115 
116 } // namespace OHOS::Ace::Framework
117 
118 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RENDER_IMAGE_H