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_RENDER_CUSTOM_PAINT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_RENDER_CUSTOM_PAINT_H 18 19 #include "core/components/common/properties/paint_state.h" 20 #include "core/components/custom_paint/canvas_render_context_base.h" 21 #include "core/components/custom_paint/custom_paint_component.h" 22 #include "core/components/custom_paint/offscreen_canvas.h" 23 24 #include "core/pipeline/base/render_node.h" 25 26 namespace OHOS::Ace { 27 28 class RenderOffscreenCanvas : public OffscreenCanvas { 29 DECLARE_ACE_TYPE(RenderOffscreenCanvas, OffscreenCanvas); 30 public: 31 static RefPtr<OffscreenCanvas> Create(const WeakPtr<PipelineContext>& context, int32_t width, int32_t height); 32 }; 33 34 class RenderCustomPaint : public RenderNode { 35 DECLARE_ACE_TYPE(RenderCustomPaint, RenderNode); 36 37 public: 38 ~RenderCustomPaint() override = default; 39 40 static RefPtr<RenderNode> Create(); 41 void Update(const RefPtr<Component>& component) override; 42 void PerformLayout() override; 43 44 void PushTask(const TaskFunc& func); 45 HasTask()46 bool HasTask() const 47 { 48 return !tasks_.empty(); 49 } 50 GetTasks()51 const std::list<TaskFunc>& GetTasks() const 52 { 53 return tasks_; 54 } 55 SetTasks(const std::list<TaskFunc> & tasks)56 void SetTasks(const std::list<TaskFunc>& tasks) 57 { 58 tasks_ = tasks; 59 } 60 61 void FlushPipelineImmediately(); 62 63 virtual void TransferFromImageBitmap(const RefPtr<OffscreenCanvas>& offscreenCanvas) = 0; 64 virtual void DrawBitmapMesh(const RefPtr<OffscreenCanvas>& offscreenCanvas, 65 const std::vector<double>& mesh, int32_t column, int32_t row) = 0; 66 virtual std::string ToDataURL(const std::string& args) = 0; 67 virtual void SetAntiAlias(bool isEnabled) = 0; 68 virtual void FillRect(const Offset& offset, const Rect& rect) = 0; 69 virtual void StrokeRect(const Offset& offset, const Rect& rect) = 0; 70 virtual void ClearRect(const Offset& offset, const Rect& rect) = 0; 71 72 virtual void FillText(const Offset& offset, const std::string& text, double x, double y) = 0; 73 virtual void StrokeText(const Offset& offset, const std::string& text, double x, double y) = 0; 74 virtual double MeasureText(const std::string& text, const PaintState& state) = 0; 75 virtual double MeasureTextHeight(const std::string& text, const PaintState& state) = 0; 76 virtual TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) = 0; 77 78 virtual void MoveTo(const Offset& offset, double x, double y) = 0; 79 virtual void LineTo(const Offset& offset, double x, double y) = 0; 80 virtual void BezierCurveTo(const Offset& offset, const BezierCurveParam& param) = 0; 81 virtual void QuadraticCurveTo(const Offset& offset, const QuadraticCurveParam& param) = 0; 82 virtual void Arc(const Offset& offset, const ArcParam& param) = 0; 83 virtual void ArcTo(const Offset& offset, const ArcToParam& param) = 0; 84 virtual void Ellipse(const Offset& offset, const EllipseParam& param) = 0; 85 virtual void AddRect(const Offset& offset, const Rect& rect) = 0; 86 87 virtual void Fill(const Offset& offset) = 0; 88 virtual void Stroke(const Offset& offset) = 0; 89 virtual void Stroke(const Offset& offset, const RefPtr<CanvasPath2D>& path) = 0; 90 virtual void Clip() = 0; 91 virtual void BeginPath() = 0; 92 virtual void ClosePath() = 0; 93 94 // in render node, restore and save are used for clip and matrix operation 95 virtual void Restore() = 0; 96 virtual void Save() = 0; 97 98 virtual void Rotate(double angle) = 0; 99 virtual void Scale(double x, double y) = 0; 100 virtual void SetTransform(const TransformParam& param) = 0; 101 virtual void Transform(const TransformParam& param) = 0; 102 virtual void Translate(double x, double y) = 0; 103 virtual void DrawImage(const Offset& offset, const CanvasImage& image, double width, double height) = 0; 104 virtual void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& canvasImage) = 0; 105 virtual void PutImageData(const Offset& offset, const ImageData& imageData) = 0; 106 virtual std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) = 0; 107 virtual std::string GetJsonData(const std::string& path) = 0; 108 109 virtual void WebGLInit(CanvasRenderContextBase* context) = 0; 110 virtual void WebGLUpdate() = 0; 111 IsRepaintBoundary()112 bool IsRepaintBoundary() const override 113 { 114 return true; 115 } 116 SetFillColor(const Color & color)117 void SetFillColor(const Color& color) 118 { 119 fillState_.SetColor(color); 120 fillState_.SetTextColor(color); 121 } 122 SetStrokeColor(const Color & color)123 void SetStrokeColor(const Color& color) 124 { 125 strokeState_.SetColor(color); 126 } 127 SetFillGradient(const Gradient & gradient)128 void SetFillGradient(const Gradient& gradient) 129 { 130 fillState_.SetGradient(gradient); 131 } 132 SetStrokeGradient(const Gradient & gradient)133 void SetStrokeGradient(const Gradient& gradient) 134 { 135 strokeState_.SetGradient(gradient); 136 } 137 SetFillPattern(const Pattern & pattern)138 void SetFillPattern(const Pattern& pattern) 139 { 140 fillState_.SetPattern(pattern); 141 } 142 SetStrokePattern(const Pattern & pattern)143 void SetStrokePattern(const Pattern& pattern) 144 { 145 strokeState_.SetPattern(pattern); 146 } 147 SetShadowBlur(double blur)148 void SetShadowBlur(double blur) 149 { 150 shadow_.SetBlurRadius(blur); 151 } 152 SetShadowOffsetX(double x)153 void SetShadowOffsetX(double x) 154 { 155 shadow_.SetOffsetX(x); 156 } 157 SetShadowOffsetY(double y)158 void SetShadowOffsetY(double y) 159 { 160 shadow_.SetOffsetY(y); 161 } 162 SetSmoothingEnabled(bool enabled)163 void SetSmoothingEnabled(bool enabled) 164 { 165 smoothingEnabled_ = enabled; 166 } 167 SetSmoothingQuality(const std::string & quality)168 void SetSmoothingQuality(const std::string& quality) 169 { 170 smoothingQuality_ = quality; 171 } 172 SetShadowColor(const Color & color)173 void SetShadowColor(const Color& color) 174 { 175 shadow_.SetColor(color); 176 } 177 SetAlpha(double alpha)178 void SetAlpha(double alpha) 179 { 180 globalState_.SetAlpha(alpha); 181 } 182 SetCompositeType(CompositeOperation operation)183 void SetCompositeType(CompositeOperation operation) 184 { 185 globalState_.SetType(operation); 186 } 187 SetLineDashOffset(double offset)188 void SetLineDashOffset(double offset) 189 { 190 strokeState_.SetLineDashOffset(offset); 191 } 192 SetLineDash(const std::vector<double> & segments)193 void SetLineDash(const std::vector<double>& segments) 194 { 195 strokeState_.SetLineDash(segments); 196 } 197 SetTextAlign(TextAlign align)198 void SetTextAlign(TextAlign align) 199 { 200 fillState_.SetTextAlign(align); 201 strokeState_.SetTextAlign(align); 202 } 203 SetTextBaseline(TextBaseline baseline)204 void SetTextBaseline(TextBaseline baseline) 205 { 206 fillState_.SetTextBaseline(baseline); 207 strokeState_.SetTextBaseline(baseline); 208 } 209 SetFontWeight(FontWeight weight)210 void SetFontWeight(FontWeight weight) 211 { 212 fillState_.SetFontWeight(weight); 213 strokeState_.SetFontWeight(weight); 214 } 215 SetFontFamilies(const std::vector<std::string> & fontFamilies)216 void SetFontFamilies(const std::vector<std::string>& fontFamilies) 217 { 218 fillState_.SetFontFamilies(fontFamilies); 219 strokeState_.SetFontFamilies(fontFamilies); 220 } 221 SetFontStyle(FontStyle style)222 void SetFontStyle(FontStyle style) 223 { 224 fillState_.SetFontStyle(style); 225 strokeState_.SetFontStyle(style); 226 } 227 SetFontSize(const Dimension & size)228 void SetFontSize(const Dimension& size) 229 { 230 fillState_.SetFontSize(size); 231 strokeState_.SetFontSize(size); 232 } 233 SetTextStyle(const TextStyle & style)234 void SetTextStyle(const TextStyle& style) 235 { 236 fillState_.SetTextStyle(style); 237 strokeState_.SetTextStyle(style); 238 } 239 SetLineWidth(double width)240 void SetLineWidth(double width) 241 { 242 strokeState_.SetLineWidth(width); 243 } 244 SetLineCap(LineCapStyle style)245 void SetLineCap(LineCapStyle style) 246 { 247 strokeState_.SetLineCap(style); 248 } 249 SetLineJoin(LineJoinStyle style)250 void SetLineJoin(LineJoinStyle style) 251 { 252 strokeState_.SetLineJoin(style); 253 } 254 SetMiterLimit(double limit)255 void SetMiterLimit(double limit) 256 { 257 strokeState_.SetMiterLimit(limit); 258 } 259 SaveStates()260 void SaveStates() 261 { 262 PaintHolder holder; 263 holder.shadow = shadow_; 264 holder.fillState = fillState_; 265 holder.globalState = globalState_; 266 holder.strokeState = strokeState_; 267 saveStates_.push(holder); 268 } 269 RestoreStates()270 void RestoreStates() 271 { 272 if (saveStates_.empty()) { 273 return; 274 } 275 auto saveState = saveStates_.top(); 276 shadow_ = saveState.shadow; 277 fillState_ = saveState.fillState; 278 strokeState_ = saveState.strokeState; 279 globalState_ = saveState.globalState; 280 saveStates_.pop(); 281 } 282 SetWebGLInstance(CanvasRenderContextBase * context)283 void SetWebGLInstance(CanvasRenderContextBase* context) 284 { 285 webGLContext_ = context; 286 } 287 GetWebGLInstance()288 CanvasRenderContextBase* GetWebGLInstance() const 289 { 290 return webGLContext_; 291 } 292 293 protected: 294 RenderCustomPaint(); 295 296 PaintState fillState_; 297 StrokePaintState strokeState_; 298 299 // save alpha and compositeType in GlobalPaintState 300 GlobalPaintState globalState_; 301 Shadow shadow_; 302 303 bool smoothingEnabled_ = true; 304 std::string smoothingQuality_ = "low"; 305 306 // PaintHolder includes fillState, strokeState, globalState and shadow for save 307 std::stack<PaintHolder> saveStates_; 308 309 RefPtr<CanvasTaskPool> pool_; 310 std::list<TaskFunc> tasks_; 311 312 ContextType type_ = ContextType::RENDER_2D; 313 CanvasRenderContextBase* webGLContext_ = nullptr; 314 315 private: 316 Dimension width_; 317 Dimension height_; 318 }; 319 320 } // namespace OHOS::Ace 321 322 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_RENDER_CUSTOM_PAINT_H 323