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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_V8_V8_CANVAS_BRIDGE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_V8_V8_CANVAS_BRIDGE_H 18 19 #include <unordered_map> 20 21 #include "third_party/v8/include/v8.h" 22 23 #include "base/memory/ace_type.h" 24 #include "core/components/common/properties/decoration.h" 25 #include "core/components/custom_paint/canvas_render_context_base.h" 26 #include "frameworks/bridge/common/dom/dom_canvas.h" 27 #include "frameworks/bridge/js_frontend/engine/common/base_canvas_bridge.h" 28 #include "frameworks/bridge/js_frontend/engine/v8/v8_engine.h" 29 30 namespace OHOS::Ace::Framework { 31 32 class V8CanvasBridge : public BaseCanvasBridge { 33 DECLARE_ACE_TYPE(V8CanvasBridge, BaseCanvasBridge) 34 35 public: 36 V8CanvasBridge() = default; 37 virtual ~V8CanvasBridge(); 38 GetRenderContext()39 v8::Local<v8::Object> GetRenderContext() const 40 { 41 return renderContext_; 42 } 43 GetDataURL()44 v8::Local<v8::String> GetDataURL() const 45 { 46 return dataURL_; 47 } 48 49 void HandleContext(const v8::Local<v8::Context>& ctx, NodeId id, const std::string& args, JsEngineInstance* engine); 50 void HandleToDataURL(const v8::Local<v8::Context>& ctx, NodeId id, const std::string& args); 51 52 static void CreateLinearGradient(const v8::FunctionCallbackInfo<v8::Value>& args); 53 static void CreateRadialGradient(const v8::FunctionCallbackInfo<v8::Value>& args); 54 static void AddColorStop(const v8::FunctionCallbackInfo<v8::Value>& args); 55 56 // rect bridge 57 static void FillRect(const v8::FunctionCallbackInfo<v8::Value>& args); 58 static void StrokeRect(const v8::FunctionCallbackInfo<v8::Value>& args); 59 static void ClearRect(const v8::FunctionCallbackInfo<v8::Value>& args); 60 61 // text bridge 62 static void FillText(const v8::FunctionCallbackInfo<v8::Value>& args); 63 static void StrokeText(const v8::FunctionCallbackInfo<v8::Value>& args); 64 static void MeasureText(const v8::FunctionCallbackInfo<v8::Value>& args); 65 66 // path bridge 67 static void BeginPath(const v8::FunctionCallbackInfo<v8::Value>& args); 68 static void ClosePath(const v8::FunctionCallbackInfo<v8::Value>& args); 69 static void MoveTo(const v8::FunctionCallbackInfo<v8::Value>& args); 70 static void LineTo(const v8::FunctionCallbackInfo<v8::Value>& args); 71 static void BezierCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args); 72 static void QuadraticCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args); 73 static void Arc(const v8::FunctionCallbackInfo<v8::Value>& args); 74 static void ArcTo(const v8::FunctionCallbackInfo<v8::Value>& args); 75 static void Ellipse(const v8::FunctionCallbackInfo<v8::Value>& args); 76 77 static void DrawRect(const v8::FunctionCallbackInfo<v8::Value>& args); 78 static void Fill(const v8::FunctionCallbackInfo<v8::Value>& args); 79 static void Stroke(const v8::FunctionCallbackInfo<v8::Value>& args); 80 static void Clip(const v8::FunctionCallbackInfo<v8::Value>& args); 81 82 static void Save(const v8::FunctionCallbackInfo<v8::Value>& args); 83 static void Restore(const v8::FunctionCallbackInfo<v8::Value>& args); 84 85 static void Rotate(const v8::FunctionCallbackInfo<v8::Value>& args); 86 static void Scale(const v8::FunctionCallbackInfo<v8::Value>& args); 87 static void SetTransform(const v8::FunctionCallbackInfo<v8::Value>& args); 88 static void Transform(const v8::FunctionCallbackInfo<v8::Value>& args); 89 static void Translate(const v8::FunctionCallbackInfo<v8::Value>& args); 90 static void GetLineDash(const v8::FunctionCallbackInfo<v8::Value>& args); 91 static void SetLineDash(const v8::FunctionCallbackInfo<v8::Value>& args); 92 static void DrawImage(const v8::FunctionCallbackInfo<v8::Value>& args); 93 static void CreatePath2D(const v8::FunctionCallbackInfo<v8::Value>& args); 94 static void CreatePattern(const v8::FunctionCallbackInfo<v8::Value>& args); 95 static void CreateImageData(const v8::FunctionCallbackInfo<v8::Value>& args); 96 static void PutImageData(const v8::FunctionCallbackInfo<v8::Value>& args); 97 static void GetImageData(const v8::FunctionCallbackInfo<v8::Value>& args); 98 static void TransferFromImageBitmap(const v8::FunctionCallbackInfo<v8::Value>& args); 99 // support to read inner json data by lottie 100 static void GetJsonData(const v8::FunctionCallbackInfo<v8::Value>& args); 101 static void DrawBitmapMesh(const v8::FunctionCallbackInfo<v8::Value>& args); 102 103 // getter and setter 104 static void FillStyleGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 105 static void FillStyleSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 106 static void StrokeStyleGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 107 static void StrokeStyleSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 108 static void LineCapGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 109 static void LineCapSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 110 static void LineJoinGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 111 static void LineJoinSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 112 static void MiterLimitGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 113 static void MiterLimitSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 114 static void LineWidthGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 115 static void LineWidthSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 116 static void TextAlignGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 117 static void TextAlignSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 118 static void TextBaselineGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 119 static void TextBaselineSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 120 static void FontGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 121 static void FontSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 122 static void AlphaGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 123 static void AlphaSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 124 static void CompositeOperationGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 125 static void CompositeOperationSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 126 static void LineDashOffsetGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 127 static void LineDashOffsetSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 128 static void ShadowBlurGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 129 static void ShadowBlurSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 130 static void ShadowColorGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 131 static void ShadowColorSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 132 static void ShadowOffsetXGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 133 static void ShadowOffsetXSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 134 static void ShadowOffsetYGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 135 static void ShadowOffsetYSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 136 static void SmoothingEnabledGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 137 static void SmoothingEnabledSetter(const v8::FunctionCallbackInfo<v8::Value>& info); 138 static void SmoothingQualityGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 139 static void SmoothingQualitySetter(const v8::FunctionCallbackInfo<v8::Value>& info); 140 141 // support only read attribute for lottie 142 static void WidthGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 143 static void HeightGetter(const v8::FunctionCallbackInfo<v8::Value>& info); 144 145 static void Path2DAddPath(const v8::FunctionCallbackInfo<v8::Value>& args); 146 static void Path2DSetTransform(const v8::FunctionCallbackInfo<v8::Value>& args); 147 static void Path2DMoveTo(const v8::FunctionCallbackInfo<v8::Value>& args); 148 static void Path2DLineTo(const v8::FunctionCallbackInfo<v8::Value>& args); 149 static void Path2DArc(const v8::FunctionCallbackInfo<v8::Value>& args); 150 static void Path2DArcTo(const v8::FunctionCallbackInfo<v8::Value>& args); 151 static void Path2DQuadraticCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args); 152 static void Path2DBezierCurveTo(const v8::FunctionCallbackInfo<v8::Value>& args); 153 static void Path2DEllipse(const v8::FunctionCallbackInfo<v8::Value>& args); 154 static void Path2DRect(const v8::FunctionCallbackInfo<v8::Value>& args); 155 static void Path2DClosePath(const v8::FunctionCallbackInfo<v8::Value>& args); 156 157 private: 158 static void ParseDomImage( 159 const v8::FunctionCallbackInfo<v8::Value>& args, double& width, double& height, std::string& src); 160 static Pattern GetPattern(const v8::Local<v8::Context>& context, const v8::Local<v8::Object>& value); 161 static Gradient GetGradient(const v8::Local<v8::Context>& context, const v8::Local<v8::Object>& value); 162 static RefPtr<CanvasPath2D> GetPath2D(const v8::Local<v8::Context>& context, const v8::Local<v8::Object>& value); 163 static void ParseImageData( 164 const v8::FunctionCallbackInfo<v8::Value>& args, std::vector<std::string>& array, ImageData& imageData); 165 static void SetAntiAlias(const v8::Local<v8::Context>& ctx, NodeId id, const std::string& args); 166 static RefPtr<CanvasPath2D> MakePath2D(const v8::FunctionCallbackInfo<v8::Value>& args); 167 void HandleWebglContext( 168 const v8::Local<v8::Context>& ctx, NodeId id, const std::string& args, JsEngineInstance* engine, 169 CanvasRenderContextBase*& canvasRenderContext); 170 171 v8::Local<v8::Object> renderContext_; 172 v8::Local<v8::String> dataURL_; 173 174 CanvasRenderContextBase* webglRenderContext_ = nullptr; 175 CanvasRenderContextBase* webgl2RenderContext_ = nullptr; 176 177 static int32_t gradientCount_; 178 static int32_t patternCount_; 179 static int32_t path2dCount_; 180 static std::unordered_map<int32_t, Gradient> gradientColors_; 181 static std::unordered_map<int32_t, Pattern> pattern_; 182 static std::unordered_map<int32_t, RefPtr<CanvasPath2D>> path2Ds_; 183 }; 184 185 } // namespace OHOS::Ace::Framework 186 187 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_V8_V8_CANVAS_BRIDGE_H 188