1 /* 2 * Copyright (c) 2021 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 66 void SetRenderNode(const WeakPtr<RenderCustomPaint>& paint); 67 std::string ToDataURL(const std::string& args); 68 void SetWebGLInstance(CanvasRenderContextBase* context); 69 void WebGLInit(CanvasRenderContextBase* context); 70 void WebGLUpdate(); 71 void SetAntiAlias(bool isEnabled); 72 void FillRect(const Rect& rect); 73 void StrokeRect(const Rect& rect); 74 void ClearRect(const Rect& rect); 75 void FillText(const std::string& text, const Offset& textOffset); 76 void StrokeText(const std::string& text, const Offset& textOffset); 77 double MeasureText(const std::string& text, const PaintState& state); 78 double MeasureTextHeight(const std::string& text, const PaintState& state); 79 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state); 80 void MoveTo(double x, double y); 81 void LineTo(double x, double y); 82 void BezierCurveTo(const BezierCurveParam& param); 83 void QuadraticCurveTo(const QuadraticCurveParam& param); 84 void Arc(const ArcParam& param); 85 void AddRect(const Rect& rect); 86 void ArcTo(const ArcToParam& param); 87 void Ellipse(const EllipseParam& param); 88 void Fill(); 89 void Stroke(); 90 void Stroke(const RefPtr<CanvasPath2D>& path); 91 void Clip(); 92 void BeginPath(); 93 void ClosePath(); 94 void Restore(); 95 void Save(); 96 void Rotate(double angle); 97 void Scale(double x, double y); 98 void SetTransform(const TransformParam& param); 99 void Transform(const TransformParam& param); 100 void Translate(double x, double y); 101 void DrawImage(const CanvasImage& image, double width, double height); 102 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image); 103 void PutImageData(const ImageData& imageData); 104 std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height); 105 std::string GetJsonData(const std::string& path); 106 double GetWidth() const; 107 double GetHeight() const; 108 109 void UpdateFillColor(const Color& color); 110 void UpdateStrokeColor(const Color& color); 111 void UpdateFillGradient(const Gradient& gradient); 112 void UpdateFillPattern(const Pattern& pattern); 113 void UpdateStrokeGradient(const Gradient& gradient); 114 void UpdateStrokePattern(const Pattern& pattern); 115 void UpdateLineWidth(double width); 116 void UpdateLineCap(LineCapStyle cap); 117 void UpdateLineJoin(LineJoinStyle join); 118 void UpdateMiterLimit(double miterLimit); 119 void UpdateTextBaseline(TextBaseline baseline); 120 void UpdateTextAlign(TextAlign align); 121 void UpdateFontSize(const Dimension& size); 122 void UpdateFontFamilies(const std::vector<std::string>& fontFamilies); 123 void UpdateFontWeight(FontWeight weight); 124 void UpdateFontStyle(FontStyle style); 125 void UpdateGlobalAlpha(double alpha); 126 void UpdateLineDashOffset(double offset); 127 void UpdateLineDash(const std::vector<double>& segments); 128 void UpdateShadowBlur(double blur); 129 void UpdateShadowColor(const Color& color); 130 void UpdateShadowOffsetX(double offsetX); 131 void UpdateShadowOffsetY(double offsetY); 132 void UpdateCompositeOperation(CompositeOperation type); 133 void UpdateSmoothingEnabled(bool enabled); 134 void UpdateSmoothingQuality(const std::string& quality); 135 void TransferFromImageBitmap(const RefPtr<OffscreenCanvas>& offscreenCanvas); 136 void DrawBitmapMesh(const RefPtr<OffscreenCanvas>& offscreenCanvas, 137 const std::vector<double>& mesh, int32_t column, int32_t row); 138 139 private: 140 PushTaskFunc pushToRenderNodeFunc_; 141 std::list<TaskFunc> tasks_; 142 WeakPtr<RenderCustomPaint> renderNode_; 143 }; 144 145 class ACE_EXPORT CustomPaintComponent : public RenderComponent { 146 DECLARE_ACE_TYPE(CustomPaintComponent, RenderComponent); 147 148 public: CustomPaintComponent()149 CustomPaintComponent() : RenderComponent() 150 { 151 pool_ = AceType::MakeRefPtr<CanvasTaskPool>(); 152 } 153 154 ~CustomPaintComponent() override = default; 155 156 RefPtr<Element> CreateElement() override; 157 158 RefPtr<RenderNode> CreateRenderNode() override; 159 GetWidth()160 const Dimension& GetWidth() const 161 { 162 return width_; 163 } 164 SetWidth(const Dimension & width)165 void SetWidth(const Dimension& width) 166 { 167 width_ = width; 168 } 169 GetHeight()170 const Dimension& GetHeight() const 171 { 172 return height_; 173 } 174 SetHeight(const Dimension & height)175 void SetHeight(const Dimension& height) 176 { 177 height_ = height; 178 } 179 SetOnReadyEvent(const EventMarker & value,RefPtr<PipelineContext> context)180 void SetOnReadyEvent(const EventMarker& value, RefPtr<PipelineContext> context) 181 { 182 std::function<void()> onReady_ = AceAsyncEvent<void()>::Create(value, context); 183 if (onReady_) { 184 onReady_(); 185 } 186 } 187 GetTaskPool()188 const RefPtr<CanvasTaskPool>& GetTaskPool() const 189 { 190 return pool_; 191 } 192 193 private: 194 Dimension width_; 195 Dimension height_; 196 197 RefPtr<CanvasTaskPool> pool_; 198 }; 199 200 } // namespace OHOS::Ace 201 202 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_CUSTOM_PAINT_COMPONENT_H 203