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_CORE_COMPONENTS_CUSTOM_PAINT_CUSTOM_PAINT_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_CUSTOM_PAINT_COMPONENT_H 18 19 #include <functional> 20 #include <list> 21 22 #include "base/geometry/dimension.h" 23 #include "base/geometry/rect.h" 24 #include "base/utils/macros.h" 25 #include "core/components/common/properties/paint_state.h" 26 #include "core/components/custom_paint/canvas_render_context_base.h" 27 #include "core/pipeline/base/render_component.h" 28 #include "core/pipeline/base/render_context.h" 29 #include "core/components/custom_paint/offscreen_canvas.h" 30 31 namespace OHOS::Ace { 32 33 class RenderCustomPaint; 34 35 using TaskFunc = std::function<void(RenderCustomPaint&, const Offset&)>; 36 using PushTaskFunc = std::function<void(const TaskFunc&)>; 37 38 class ACE_EXPORT CanvasTaskPool : virtual public AceType { 39 DECLARE_ACE_TYPE(CanvasTaskPool, AceType); 40 41 public: PushTask(const TaskFunc & task)42 void PushTask(const TaskFunc& task) 43 { 44 if (pushToRenderNodeFunc_) { 45 pushToRenderNodeFunc_(task); 46 } else { 47 tasks_.emplace_back(task); 48 } 49 } 50 SetPushToRenderNodeFunc(const PushTaskFunc & pushTaskFunc)51 void SetPushToRenderNodeFunc(const PushTaskFunc& pushTaskFunc) 52 { 53 pushToRenderNodeFunc_ = pushTaskFunc; 54 } 55 GetTasks()56 const std::list<TaskFunc>& GetTasks() const 57 { 58 return tasks_; 59 } 60 ClearTasks()61 void ClearTasks() 62 { 63 tasks_.clear(); 64 } 65 SetOnReadyEvent(const std::function<void ()> & onReadyEvent)66 void SetOnReadyEvent(const std::function<void()>& onReadyEvent) 67 { 68 onReadyEvent_ = onReadyEvent; 69 } 70 GetOnReadyEvent()71 const std::function<void()>& GetOnReadyEvent() const 72 { 73 return onReadyEvent_; 74 } 75 76 void SetRenderNode(const WeakPtr<RenderCustomPaint>& paint); 77 std::string ToDataURL(const std::string& args); 78 void SetWebGLInstance(CanvasRenderContextBase* context); 79 void WebGLInit(CanvasRenderContextBase* context); 80 void WebGLUpdate(); 81 void SetAntiAlias(bool isEnabled); 82 void FillRect(const Rect& rect); 83 void StrokeRect(const Rect& rect); 84 void ClearRect(const Rect& rect); 85 void FillText(const std::string& text, const Offset& textOffset); 86 void StrokeText(const std::string& text, const Offset& textOffset); 87 double MeasureText(const std::string& text, const PaintState& state); 88 double MeasureTextHeight(const std::string& text, const PaintState& state); 89 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state); 90 void MoveTo(double x, double y); 91 void LineTo(double x, double y); 92 void BezierCurveTo(const BezierCurveParam& param); 93 void QuadraticCurveTo(const QuadraticCurveParam& param); 94 void Arc(const ArcParam& param); 95 void AddRect(const Rect& rect); 96 void ArcTo(const ArcToParam& param); 97 void Ellipse(const EllipseParam& param); 98 void Fill(); 99 void Fill(const RefPtr<CanvasPath2D>& path); 100 void Stroke(); 101 void Stroke(const RefPtr<CanvasPath2D>& path); 102 void Clip(); 103 void Clip(const RefPtr<CanvasPath2D>& path); 104 void BeginPath(); 105 void ClosePath(); 106 void Restore(); 107 void ResetTransform(); 108 void Save(); 109 void Rotate(double angle); 110 void Scale(double x, double y); 111 void SetTransform(const TransformParam& param); 112 void Transform(const TransformParam& param); 113 void Translate(double x, double y); 114 void DrawImage(const CanvasImage& image, double width, double height); 115 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image); 116 void PutImageData(const ImageData& imageData); 117 std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height); 118 std::string GetJsonData(const std::string& path); 119 double GetWidth() const; 120 double GetHeight() const; 121 LineDashParam GetLineDash() const; 122 123 void UpdateFillRuleForPath(const CanvasFillRule rule); 124 void UpdateFillRuleForPath2D(const CanvasFillRule rule); 125 void UpdateFillColor(const Color& color); 126 void UpdateStrokeColor(const Color& color); 127 void UpdateFillGradient(const Gradient& gradient); 128 void UpdateFillPattern(const Pattern& pattern); 129 void UpdateStrokeGradient(const Gradient& gradient); 130 void UpdateStrokePattern(const Pattern& pattern); 131 void UpdateLineWidth(double width); 132 void UpdateLineCap(LineCapStyle cap); 133 void UpdateLineJoin(LineJoinStyle join); 134 void UpdateMiterLimit(double miterLimit); 135 void UpdateTextBaseline(TextBaseline baseline); 136 void UpdateTextAlign(TextAlign align); 137 void UpdateFontSize(const Dimension& size); 138 void UpdateFontFamilies(const std::vector<std::string>& fontFamilies); 139 void UpdateFontWeight(FontWeight weight); 140 void UpdateFontStyle(FontStyle style); 141 void UpdateGlobalAlpha(double alpha); 142 void UpdateLineDashOffset(double offset); 143 void UpdateLineDash(const std::vector<double>& segments); 144 void UpdateShadowBlur(double blur); 145 void UpdateShadowColor(const Color& color); 146 void UpdateShadowOffsetX(double offsetX); 147 void UpdateShadowOffsetY(double offsetY); 148 void UpdateCompositeOperation(CompositeOperation type); 149 void UpdateSmoothingEnabled(bool enabled); 150 void UpdateSmoothingQuality(const std::string& quality); 151 void TransferFromImageBitmap(const RefPtr<OffscreenCanvas>& offscreenCanvas); 152 void DrawBitmapMesh(const RefPtr<OffscreenCanvas>& offscreenCanvas, 153 const std::vector<double>& mesh, int32_t column, int32_t row); 154 155 private: 156 PushTaskFunc pushToRenderNodeFunc_; 157 std::list<TaskFunc> tasks_; 158 WeakPtr<RenderCustomPaint> renderNode_; 159 std::function<void()> onReadyEvent_; 160 }; 161 162 class ACE_EXPORT CustomPaintComponent : public RenderComponent { 163 DECLARE_ACE_TYPE(CustomPaintComponent, RenderComponent); 164 165 public: CustomPaintComponent()166 CustomPaintComponent() : RenderComponent() 167 { 168 pool_ = AceType::MakeRefPtr<CanvasTaskPool>(); 169 } 170 171 ~CustomPaintComponent() override = default; 172 173 RefPtr<Element> CreateElement() override; 174 175 RefPtr<RenderNode> CreateRenderNode() override; 176 GetWidth()177 const Dimension& GetWidth() const 178 { 179 return width_; 180 } 181 SetWidth(const Dimension & width)182 void SetWidth(const Dimension& width) 183 { 184 width_ = width; 185 } 186 GetHeight()187 const Dimension& GetHeight() const 188 { 189 return height_; 190 } 191 SetHeight(const Dimension & height)192 void SetHeight(const Dimension& height) 193 { 194 height_ = height; 195 } 196 SetOnReadyEvent(const EventMarker & value,RefPtr<PipelineContext> context)197 void SetOnReadyEvent(const EventMarker& value, RefPtr<PipelineContext> context) 198 { 199 if (pool_) { 200 std::function<void()> onReadyEvent = AceAsyncEvent<void()>::Create(value, context); 201 pool_->SetOnReadyEvent(onReadyEvent); 202 } 203 } 204 GetTaskPool()205 const RefPtr<CanvasTaskPool>& GetTaskPool() const 206 { 207 return pool_; 208 } 209 210 private: 211 Dimension width_; 212 Dimension height_; 213 214 RefPtr<CanvasTaskPool> pool_; 215 }; 216 217 } // namespace OHOS::Ace 218 219 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_CUSTOM_PAINT_COMPONENT_H 220