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 #define DELETE_RETURN_NULL(var) \ 30 do { \ 31 if ((var)) { \ 32 delete var; \ 33 var = nullptr; \ 34 } \ 35 return nullptr; \ 36 } while (0) \ 37 38 void BindNativeFunction(napi_env env, napi_value object, const char* name, napi_callback func); 39 void* GetNapiCallbackInfoAndThis(napi_env env, napi_callback_info info); 40 41 class JSRenderImage : public Referenced { 42 public: 43 JSRenderImage(); 44 ~JSRenderImage() override = default; 45 46 static void JSBind(BindingTarget globalObj, void* nativeEngine = nullptr); 47 static void Finalizer(napi_env env, void* data, void* hint); 48 49 static napi_value InitImageBitmap(napi_env env); 50 static napi_value Constructor(napi_env env, napi_callback_info info); 51 static napi_value JsClose(napi_env env, napi_callback_info info); 52 static napi_value JsSetWidth(napi_env env, napi_callback_info info); 53 static napi_value JsSetHeight(napi_env env, napi_callback_info info); 54 static napi_value JsGetWidth(napi_env env, napi_callback_info info); 55 static napi_value JsGetHeight(napi_env env, napi_callback_info info); 56 static bool CreateJSRenderImage(napi_env env, RefPtr<PixelMap> pixelMap, napi_value& renderImage); 57 58 double GetWidth(); 59 void SetWidth(double width); 60 double GetHeight(); 61 void SetHeight(double height); 62 std::string GetSrc(); 63 void SetCloseCallback(std::function<void()>&& callback); GetPixelMap()64 RefPtr<PixelMap> GetPixelMap() const 65 { 66 return pixelMap_; 67 } 68 GetImageData()69 std::shared_ptr<Ace::ImageData> GetImageData() const 70 { 71 return imageData_; 72 } 73 SetImageData(const std::shared_ptr<Ace::ImageData> & imageData)74 void SetImageData(const std::shared_ptr<Ace::ImageData>& imageData) 75 { 76 imageData_ = imageData; 77 } 78 GetSvgDom()79 RefPtr<NG::SvgDomBase> GetSvgDom() 80 { 81 return svgDom_; 82 } 83 SetInstanceId(int32_t instanceId)84 void SetInstanceId(int32_t instanceId) 85 { 86 instanceId_ = instanceId; 87 } 88 GetInstanceId()89 int32_t GetInstanceId() 90 { 91 return instanceId_; 92 } 93 IsSvg()94 bool IsSvg() 95 { 96 return sourceInfo_.IsSvg(); 97 } 98 GetImageFit()99 ImageFit GetImageFit() 100 { 101 return imageFit_; 102 } 103 GetImageSize()104 NG::SizeF GetImageSize() 105 { 106 return imageSize_; 107 } 108 SetUnit(CanvasUnit unit)109 void SetUnit(CanvasUnit unit) 110 { 111 unit_ = unit; 112 } 113 GetUnit()114 CanvasUnit GetUnit() 115 { 116 return unit_; 117 } 118 GetDensity()119 double GetDensity() 120 { 121 double density = PipelineBase::GetCurrentDensity(); 122 return ((GetUnit() == CanvasUnit::DEFAULT) && !NearZero(density)) ? density : 1.0; 123 } 124 GetBindingSize()125 size_t GetBindingSize() const 126 { 127 return bindingSize_; 128 } 129 AddNativeRef()130 void AddNativeRef() 131 { 132 ++nativeRefCount_; 133 } 134 Release()135 void Release() 136 { 137 --nativeRefCount_; 138 if (nativeRefCount_ == 0) { 139 delete this; 140 } 141 } 142 143 ACE_DISALLOW_COPY_AND_MOVE(JSRenderImage); 144 private: 145 napi_value OnClose(); 146 napi_value OnGetWidth(napi_env env); 147 napi_value OnGetHeight(napi_env env); 148 napi_value OnSetWidth(); 149 napi_value OnSetHeight(); 150 151 void LoadImage(const std::string& src); 152 void LoadImage(const RefPtr<PixelMap>& pixmap); 153 void LoadImage(const ImageSourceInfo& src); 154 void OnImageDataReady(); 155 void OnImageLoadFail(const std::string& errorMsg); 156 void OnImageLoadSuccess(); 157 static bool NotFormSupport(const std::string& textString); 158 static std::string GetSrcString(napi_env env, napi_value value, size_t textLen); 159 #ifdef PIXEL_MAP_SUPPORTED 160 static RefPtr<PixelMap> GetPixelMap(napi_env env, napi_value value); 161 #endif 162 163 RefPtr<NG::CanvasImage> image_; 164 RefPtr<NG::ImageObject> imageObj_; 165 RefPtr<NG::ImageLoadingContext> loadingCtx_; 166 RefPtr<PixelMap> pixelMap_; 167 std::shared_ptr<Ace::ImageData> imageData_; 168 RefPtr<NG::SvgDomBase> svgDom_; 169 ImageSourceInfo sourceInfo_; 170 ImageFit imageFit_ = ImageFit::NONE; 171 NG::SizeF imageSize_; 172 173 std::string src_; 174 std::list<std::function<void()>> closeCallbacks_; 175 double width_ = 0; 176 double height_ = 0; 177 int32_t instanceId_ = 0; 178 CanvasUnit unit_ = CanvasUnit::DEFAULT; 179 size_t bindingSize_ = 0; 180 uint32_t nativeRefCount_ = 0; 181 }; 182 183 } // namespace OHOS::Ace::Framework 184 185 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RENDER_IMAGE_H 186