1 /* 2 * Copyright (c) 2024 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_NG_PATTERN_CUSTOM_PAINT_CANVAS_PAINT_OP_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CANVAS_PAINT_OP_H 18 19 #include <memory> 20 #include <optional> 21 22 #include "core/components/common/properties/paint_state.h" 23 #include "core/components_ng/pattern/canvas/canvas_paint_mem.h" 24 25 namespace OHOS::Ace { 26 struct BezierCurveParam; 27 class Rect; 28 } // namespace OHOS::Ace 29 30 namespace OHOS::Ace::NG { 31 class CanvasPaintMethod; 32 class OffscreenCanvasPattern; 33 class SvgDomBase; 34 35 #define X(T) T, 36 enum class Type : uint8_t { 37 #include "canvas_paint_ops.in" 38 }; 39 #undef X 40 41 struct Op { 42 uint32_t type : 8; 43 uint32_t skip : 24; 44 }; 45 static_assert(sizeof(Op) == 4, "size of canvas op shoule be 4"); 46 47 struct SaveOp final : Op { 48 static constexpr auto kType = Type::SaveOp; 49 void Draw(CanvasPaintMethod* method) const; 50 }; 51 52 struct RestoreOp final : Op { 53 static constexpr auto kType = Type::RestoreOp; 54 void Draw(CanvasPaintMethod* method) const; 55 }; 56 57 struct FillRectOp final : Op { 58 static constexpr auto kType = Type::FillRectOp; FillRectOpfinal59 explicit FillRectOp(const Rect& rect): rect(std::move(rect)) {} 60 Rect rect; 61 void Draw(CanvasPaintMethod* method) const; 62 }; 63 64 struct FillTextOp final : Op { 65 static constexpr auto kType = Type::FillTextOp; FillTextOpfinal66 FillTextOp(const std::string& text, double x, double y, std::optional<double> maxWidth) 67 : text(std::move(text)), x(x), y(y), maxWidth(maxWidth) {} 68 std::string text; 69 double x, y; 70 std::optional<double> maxWidth; 71 void Draw(CanvasPaintMethod* method) const; 72 }; 73 74 struct BezierCurveToOp final : Op { 75 static constexpr auto kType = Type::BezierCurveToOp; BezierCurveToOpfinal76 explicit BezierCurveToOp(const BezierCurveParam& param): param(std::move(param)) {} 77 BezierCurveParam param; 78 void Draw(CanvasPaintMethod* method) const; 79 }; 80 81 struct SetTransformOp final : Op { 82 static constexpr auto kType = Type::SetTransformOp; SetTransformOpfinal83 explicit SetTransformOp(const TransformParam& param): param(std::move(param)) {} 84 TransformParam param; 85 void Draw(CanvasPaintMethod* method) const; 86 }; 87 88 struct SetFillColorOp final : Op { 89 static constexpr auto kType = Type::SetFillColorOp; SetFillColorOpfinal90 explicit SetFillColorOp(const Color& color): color(std::move(color)) {} 91 Color color; 92 void Draw(CanvasPaintMethod* method) const; 93 }; 94 95 struct SetFillGradientOp final : Op { 96 static constexpr auto kType = Type::SetFillGradientOp; SetFillGradientOpfinal97 explicit SetFillGradientOp(const std::shared_ptr<Ace::Gradient>& gradient): gradient(gradient) {} 98 std::weak_ptr<Ace::Gradient> gradient; 99 void Draw(CanvasPaintMethod* method) const; 100 }; 101 102 struct SetFillPatternNGOp final : Op { 103 static constexpr auto kType = Type::SetFillPatternNGOp; SetFillPatternNGOpfinal104 explicit SetFillPatternNGOp(const std::weak_ptr<Ace::Pattern>& pattern): pattern(pattern) {} 105 std::weak_ptr<Ace::Pattern> pattern; 106 void Draw(CanvasPaintMethod* method) const; 107 }; 108 109 struct SetAlphaOp final : Op { 110 static constexpr auto kType = Type::SetAlphaOp; SetAlphaOpfinal111 explicit SetAlphaOp(double alpha): alpha(alpha) {} 112 double alpha; 113 void Draw(CanvasPaintMethod* method) const; 114 }; 115 116 struct SetFillRuleForPathOp final : Op { 117 static constexpr auto kType = Type::SetFillRuleForPathOp; SetFillRuleForPathOpfinal118 explicit SetFillRuleForPathOp(const CanvasFillRule& rule): rule(std::move(rule)) {} 119 CanvasFillRule rule; 120 void Draw(CanvasPaintMethod* method) const; 121 }; 122 123 struct SetFillRuleForPath2DOp final : Op { 124 static constexpr auto kType = Type::SetFillRuleForPath2DOp; SetFillRuleForPath2DOpfinal125 explicit SetFillRuleForPath2DOp(const CanvasFillRule& rule): rule(rule) {} 126 CanvasFillRule rule; 127 void Draw(CanvasPaintMethod* method) const; 128 }; 129 130 struct FillOp final : Op { 131 static constexpr auto kType = Type::FillOp; 132 void Draw(CanvasPaintMethod* method) const; 133 }; 134 135 struct Fill2DOp final : Op { 136 static constexpr auto kType = Type::Fill2DOp; Fill2DOpfinal137 explicit Fill2DOp(const RefPtr<CanvasPath2D>& path): path(path) {} 138 RefPtr<CanvasPath2D> path; 139 void Draw(CanvasPaintMethod* method) const; 140 }; 141 142 struct MoveToOp final : Op { 143 static constexpr auto kType = Type::MoveToOp; MoveToOpfinal144 MoveToOp(double x, double y): x(x), y(y) {} 145 double x, y; 146 void Draw(CanvasPaintMethod* method) const; 147 }; 148 149 struct BeginPathOp final : Op { 150 static constexpr auto kType = Type::BeginPathOp; 151 void Draw(CanvasPaintMethod* method) const; 152 }; 153 154 struct SetFilterParamOp : Op { 155 static constexpr auto kType = Type::SetFilterParamOp; SetFilterParamOpSetFilterParamOp156 explicit SetFilterParamOp(const std::string& filterStr): filterStr(std::move(filterStr)) {} 157 std::string filterStr; 158 void Draw(CanvasPaintMethod* method) const; 159 }; 160 161 struct ClosePathOp final : Op { 162 static constexpr auto kType = Type::ClosePathOp; 163 void Draw(CanvasPaintMethod* method) const; 164 }; 165 166 struct ClearRectOp final : Op { 167 static constexpr auto kType = Type::ClearRectOp; ClearRectOpfinal168 explicit ClearRectOp(const Rect& rect): rect(std::move(rect)) {} 169 Rect rect; 170 void Draw(CanvasPaintMethod* method) const; 171 }; 172 173 struct SetStrokeColorOp final : Op { 174 static constexpr auto kType = Type::SetStrokeColorOp; SetStrokeColorOpfinal175 explicit SetStrokeColorOp(const Color& color): color(std::move(color)) {} 176 Color color; 177 void Draw(CanvasPaintMethod* method) const; 178 }; 179 180 struct SetStrokeGradientOp final : Op { 181 static constexpr auto kType = Type::SetStrokeGradientOp; SetStrokeGradientOpfinal182 explicit SetStrokeGradientOp(const std::shared_ptr<Ace::Gradient>& gradient): gradient(gradient) {} 183 std::weak_ptr<Ace::Gradient> gradient; 184 void Draw(CanvasPaintMethod* method) const; 185 }; 186 187 struct SetStrokePatternNGOp final : Op { 188 static constexpr auto kType = Type::SetStrokePatternNGOp; SetStrokePatternNGOpfinal189 explicit SetStrokePatternNGOp(const std::weak_ptr<Ace::Pattern>& pattern): pattern(pattern) {} 190 std::weak_ptr<Ace::Pattern> pattern; 191 void Draw(CanvasPaintMethod* method) const; 192 }; 193 194 struct LineToOp final : Op { 195 static constexpr auto kType = Type::LineToOp; LineToOpfinal196 LineToOp(double x, double y): x(x), y(y) {} 197 double x, y; 198 void Draw(CanvasPaintMethod* method) const; 199 }; 200 201 struct SetLineJoinOp final : Op { 202 static constexpr auto kType = Type::SetLineJoinOp; SetLineJoinOpfinal203 explicit SetLineJoinOp(LineJoinStyle style): style(std::move(style)) {} 204 LineJoinStyle style; 205 void Draw(CanvasPaintMethod* method) const; 206 }; 207 208 struct SetLineCapOp final : Op { 209 static constexpr auto kType = Type::SetLineCapOp; SetLineCapOpfinal210 explicit SetLineCapOp(LineCapStyle style): style(std::move(style)) {} 211 LineCapStyle style; 212 void Draw(CanvasPaintMethod* method) const; 213 }; 214 struct SetLineWidthOp final : Op { 215 static constexpr auto kType = Type::SetLineWidthOp; SetLineWidthOpfinal216 explicit SetLineWidthOp(double width): width(width) {} 217 double width; 218 void Draw(CanvasPaintMethod* method) const; 219 }; 220 221 struct SetMiterLimitOp final : Op { 222 static constexpr auto kType = Type::SetMiterLimitOp; SetMiterLimitOpfinal223 explicit SetMiterLimitOp(double limit): limit(limit) {} 224 double limit; 225 void Draw(CanvasPaintMethod* method) const; 226 }; 227 228 struct StrokeOp final : Op { 229 static constexpr auto kType = Type::StrokeOp; 230 void Draw(CanvasPaintMethod* method) const; 231 }; 232 233 struct Stroke2DOp final : Op { 234 static constexpr auto kType = Type::Stroke2DOp; Stroke2DOpfinal235 explicit Stroke2DOp(const RefPtr<CanvasPath2D>& path): path(path) {} 236 RefPtr<CanvasPath2D> path; 237 void Draw(CanvasPaintMethod* method) const; 238 }; 239 240 struct SetCompositeTypeOp final : Op { 241 static constexpr auto kType = Type::SetCompositeTypeOp; SetCompositeTypeOpfinal242 explicit SetCompositeTypeOp(CompositeOperation operation): operation(std::move(operation)) {} 243 CompositeOperation operation; 244 void Draw(CanvasPaintMethod* method) const; 245 }; 246 247 struct ClipOp final : Op { 248 static constexpr auto kType = Type::ClipOp; 249 void Draw(CanvasPaintMethod* method) const; 250 }; 251 252 struct Clip2DOp final : Op { 253 static constexpr auto kType = Type::Clip2DOp; Clip2DOpfinal254 explicit Clip2DOp(const RefPtr<CanvasPath2D>& path): path(path) {} 255 RefPtr<CanvasPath2D> path; 256 void Draw(CanvasPaintMethod* method) const; 257 }; 258 259 struct StrokeRectOp final : Op { 260 static constexpr auto kType = Type::StrokeRectOp; StrokeRectOpfinal261 explicit StrokeRectOp(const Rect& rect): rect(std::move(rect)) {} 262 Rect rect; 263 void Draw(CanvasPaintMethod* method) const; 264 }; 265 266 struct ArcOp final : Op { 267 static constexpr auto kType = Type::ArcOp; ArcOpfinal268 explicit ArcOp(const ArcParam& param): param(std::move(param)) {} 269 ArcParam param; 270 void Draw(CanvasPaintMethod* method) const; 271 }; 272 273 struct ArcToOp final : Op { 274 static constexpr auto kType = Type::ArcToOp; ArcToOpfinal275 explicit ArcToOp(const ArcToParam& param): param(std::move(param)) {} 276 ArcToParam param; 277 void Draw(CanvasPaintMethod* method) const; 278 }; 279 280 struct AddRectOp final : Op { 281 static constexpr auto kType = Type::AddRectOp; AddRectOpfinal282 explicit AddRectOp(const Rect& rect): rect(std::move(rect)) {} 283 Rect rect; 284 void Draw(CanvasPaintMethod* method) const; 285 }; 286 287 struct EllipseOp final : Op { 288 static constexpr auto kType = Type::EllipseOp; EllipseOpfinal289 explicit EllipseOp(const EllipseParam& param): param(std::move(param)) {} 290 EllipseParam param; 291 void Draw(CanvasPaintMethod* method) const; 292 }; 293 294 struct QuadraticCurveToOp final : Op { 295 static constexpr auto kType = Type::QuadraticCurveToOp; QuadraticCurveToOpfinal296 explicit QuadraticCurveToOp(const QuadraticCurveParam& param): param(std::move(param)) {} 297 QuadraticCurveParam param; 298 void Draw(CanvasPaintMethod* method) const; 299 }; 300 301 struct PutImageDataOp final : Op { 302 static constexpr auto kType = Type::PutImageDataOp; PutImageDataOpfinal303 explicit PutImageDataOp(const Ace::ImageData& imageData): imageData(std::move(imageData)) {} 304 Ace::ImageData imageData; 305 void Draw(CanvasPaintMethod* method) const; 306 }; 307 308 struct ScaleOp final : Op { 309 static constexpr auto kType = Type::ScaleOp; ScaleOpfinal310 ScaleOp(double x, double y): x(x), y(y) {} 311 double x; 312 double y; 313 void Draw(CanvasPaintMethod* method) const; 314 }; 315 316 struct RotateOp final : Op { 317 static constexpr auto kType = Type::RotateOp; RotateOpfinal318 explicit RotateOp(double angle): angle(angle) {} 319 double angle; 320 void Draw(CanvasPaintMethod* method) const; 321 }; 322 323 struct TransformOp final : Op { 324 static constexpr auto kType = Type::TransformOp; TransformOpfinal325 explicit TransformOp(const TransformParam& param): param(std::move(param)) {} 326 TransformParam param; 327 void Draw(CanvasPaintMethod* method) const; 328 }; 329 330 struct TranslateOp final : Op { 331 static constexpr auto kType = Type::TranslateOp; TranslateOpfinal332 TranslateOp(double x, double y): x(x), y(y) {} 333 double x; 334 double y; 335 void Draw(CanvasPaintMethod* method) const; 336 }; 337 338 struct SaveLayerOp final : Op { 339 static constexpr auto kType = Type::SaveLayerOp; 340 void Draw(CanvasPaintMethod* method) const; 341 }; 342 343 struct RestoreLayerOp final : Op { 344 static constexpr auto kType = Type::RestoreLayerOp; 345 void Draw(CanvasPaintMethod* method) const; 346 }; 347 348 struct SetAntiAliasOp final : Op { 349 static constexpr auto kType = Type::SetAntiAliasOp; SetAntiAliasOpfinal350 explicit SetAntiAliasOp(bool isEnabled): isEnabled(isEnabled) {} 351 bool isEnabled; 352 void Draw(CanvasPaintMethod* method) const; 353 }; 354 355 struct SetTextDirectionOp final : Op { 356 static constexpr auto kType = Type::SetTextDirectionOp; SetTextDirectionOpfinal357 explicit SetTextDirectionOp(TextDirection direction): direction(std::move(direction)) {} 358 TextDirection direction; 359 void Draw(CanvasPaintMethod* method) const; 360 }; 361 362 struct SetLineDashOffsetOp final : Op { 363 static constexpr auto kType = Type::SetLineDashOffsetOp; SetLineDashOffsetOpfinal364 explicit SetLineDashOffsetOp(double offset): offset(offset) {} 365 double offset; 366 void Draw(CanvasPaintMethod* method) const; 367 }; 368 369 struct SetLineDashOp final : Op { 370 static constexpr auto kType = Type::SetLineDashOp; SetLineDashOpfinal371 explicit SetLineDashOp(const std::vector<double>& segments): segments(std::move(segments)) {} 372 std::vector<double> segments; 373 void Draw(CanvasPaintMethod* method) const; 374 }; 375 376 struct SetTextAlignOp final : Op { 377 static constexpr auto kType = Type::SetTextAlignOp; SetTextAlignOpfinal378 explicit SetTextAlignOp(TextAlign align): align(std::move(align)) {} 379 TextAlign align; 380 void Draw(CanvasPaintMethod* method) const; 381 }; 382 383 struct SetTextBaselineOp final : Op { 384 static constexpr auto kType = Type::SetTextBaselineOp; SetTextBaselineOpfinal385 explicit SetTextBaselineOp(TextBaseline baseline): baseline(std::move(baseline)) {} 386 TextBaseline baseline; 387 void Draw(CanvasPaintMethod* method) const; 388 }; 389 390 struct SetShadowColorOp final : Op { 391 static constexpr auto kType = Type::SetShadowColorOp; SetShadowColorOpfinal392 explicit SetShadowColorOp(const Color& color): color(std::move(color)) {} 393 Color color; 394 void Draw(CanvasPaintMethod* method) const; 395 }; 396 397 struct SetShadowBlurOp final : Op { 398 static constexpr auto kType = Type::SetShadowBlurOp; SetShadowBlurOpfinal399 explicit SetShadowBlurOp(double blur): blur(blur) {} 400 double blur; 401 void Draw(CanvasPaintMethod* method) const; 402 }; 403 404 struct SetShadowOffsetXOp final : Op { 405 static constexpr auto kType = Type::SetShadowOffsetXOp; SetShadowOffsetXOpfinal406 explicit SetShadowOffsetXOp(double x): x(x) {} 407 double x; 408 void Draw(CanvasPaintMethod* method) const; 409 }; 410 411 struct SetShadowOffsetYOp final : Op { 412 static constexpr auto kType = Type::SetShadowOffsetYOp; SetShadowOffsetYOpfinal413 explicit SetShadowOffsetYOp(double y): y(y) {} 414 double y; 415 void Draw(CanvasPaintMethod* method) const; 416 }; 417 418 struct SetSmoothingEnabledOp final : Op { 419 static constexpr auto kType = Type::SetSmoothingEnabledOp; SetSmoothingEnabledOpfinal420 explicit SetSmoothingEnabledOp(bool enabled): enabled(enabled) {} 421 bool enabled; 422 void Draw(CanvasPaintMethod* method) const; 423 }; 424 425 struct SetSmoothingQualityOp final : Op { 426 static constexpr auto kType = Type::SetSmoothingQualityOp; SetSmoothingQualityOpfinal427 explicit SetSmoothingQualityOp(const std::string& quality): quality(std::move(quality)) {} 428 std::string quality; 429 void Draw(CanvasPaintMethod* method) const; 430 }; 431 432 struct SetFontSizeOp final : Op { 433 static constexpr auto kType = Type::SetFontSizeOp; SetFontSizeOpfinal434 explicit SetFontSizeOp(const Dimension& size): size(std::move(size)) {} 435 Dimension size; 436 void Draw(CanvasPaintMethod* method) const; 437 }; 438 439 struct SetFontStyleOp final : Op { 440 static constexpr auto kType = Type::SetFontStyleOp; SetFontStyleOpfinal441 explicit SetFontStyleOp(OHOS::Ace::FontStyle style): style(std::move(style)) {} 442 OHOS::Ace::FontStyle style; 443 void Draw(CanvasPaintMethod* method) const; 444 }; 445 446 struct SetFontWeightOp final : Op { 447 static constexpr auto kType = Type::SetFontWeightOp; SetFontWeightOpfinal448 explicit SetFontWeightOp(FontWeight weight): weight(std::move(weight)) {} 449 FontWeight weight; 450 void Draw(CanvasPaintMethod* method) const; 451 }; 452 453 struct SetFontFamiliesOp final : Op { 454 static constexpr auto kType = Type::SetFontFamiliesOp; SetFontFamiliesOpfinal455 explicit SetFontFamiliesOp(const std::vector<std::string>& fontFamilies) : fontFamilies(std::move(fontFamilies)) {} 456 std::vector<std::string> fontFamilies; 457 void Draw(CanvasPaintMethod* method) const; 458 }; 459 460 struct DrawSvgImageOp final : Op { 461 static constexpr auto kType = Type::DrawSvgImageOp; DrawSvgImageOpfinal462 DrawSvgImageOp(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& canvasImage, const ImageFit& imageFit) 463 : svgDom(svgDom), canvasImage(std::move(canvasImage)), imageFit(std::move(imageFit)) {} 464 RefPtr<SvgDomBase> svgDom; 465 Ace::CanvasImage canvasImage; 466 ImageFit imageFit; 467 void Draw(CanvasPaintMethod* method) const; 468 }; 469 470 struct DrawImageOp final : Op { 471 static constexpr auto kType = Type::DrawImageOp; DrawImageOpfinal472 DrawImageOp(const Ace::CanvasImage& canvasImage, double width, double height) 473 : canvasImage(std::move(canvasImage)), width(width), height(height) {} 474 Ace::CanvasImage canvasImage; 475 double width; 476 double height; 477 void Draw(CanvasPaintMethod* method) const; 478 }; 479 480 struct DrawPixelMapOp final : Op { 481 static constexpr auto kType = Type::DrawPixelMapOp; DrawPixelMapOpfinal482 DrawPixelMapOp(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& canvasImage) 483 : pixelMap(pixelMap), canvasImage(std::move(canvasImage)) {} 484 RefPtr<PixelMap> pixelMap; 485 Ace::CanvasImage canvasImage; 486 void Draw(CanvasPaintMethod* method) const; 487 }; 488 489 #ifdef PIXEL_MAP_SUPPORTED 490 struct TransferFromImageBitmapOp final : Op { 491 static constexpr auto kType = Type::TransferFromImageBitmapOp; TransferFromImageBitmapOpfinal492 explicit TransferFromImageBitmapOp(const RefPtr<PixelMap>& pixelMap) 493 : pixelMap(pixelMap) {} 494 RefPtr<PixelMap> pixelMap; 495 void Draw(CanvasPaintMethod* method) const; 496 }; 497 #endif 498 499 struct StrokeTextOp final : Op { 500 static constexpr auto kType = Type::StrokeTextOp; StrokeTextOpfinal501 StrokeTextOp(const std::string& text, double x, double y, std::optional<double> maxWidth) 502 : text(std::move(text)), x(x), y(y), maxWidth(maxWidth) {} 503 std::string text; 504 double x; 505 double y; 506 std::optional<double> maxWidth; 507 void Draw(CanvasPaintMethod* method) const; 508 }; 509 510 struct ResetCanvasOp final : Op { 511 static constexpr auto kType = Type::ResetCanvasOp; 512 void Draw(CanvasPaintMethod* method) const; 513 }; 514 515 struct ResetTransformOp final : Op { 516 static constexpr auto kType = Type::ResetTransformOp; 517 void Draw(CanvasPaintMethod* method) const; 518 }; 519 520 struct SetInvalidateOp final : Op { 521 static constexpr auto kType = Type::SetInvalidateOp; 522 void Draw(CanvasPaintMethod* method) const; 523 }; 524 525 typedef void (*DrawFn)(const void*, CanvasPaintMethod* method); 526 typedef void (*VoidFn)(const void*); 527 528 #define X(T) \ 529 [](const void* op, CanvasPaintMethod* method) { \ 530 ((const T*)op)->Draw(method); \ 531 }, 532 static const DrawFn DRAW_FNS[] = { 533 #include "./canvas_paint_ops.in" 534 }; 535 #undef X 536 537 #define X(T) \ 538 !std::is_trivially_destructible<T>::value ? [](const void* op) { ((const T*)op)->~T(); } \ 539 : (VoidFn) nullptr, 540 static const VoidFn DTOR_FBS[] = { 541 #include "./canvas_paint_ops.in" 542 }; 543 #undef X 544 545 class CanvasPaintOp final { 546 public: CanvasPaintOp()547 CanvasPaintOp() : mHasText(false) {} ~CanvasPaintOp()548 ~CanvasPaintOp() { Reset(); } 549 550 void Draw(CanvasPaintMethod* method) const; 551 void Reset(); Empty()552 bool Empty() const {return fUsed == 0; } 553 HasText()554 bool HasText() const { return mHasText; } UsedSize()555 size_t UsedSize() const { return fUsed; } AllocatedSize()556 size_t AllocatedSize() const { return fReserved; } 557 558 template <typename Fn, typename... Args> 559 void Map(const Fn[], Args...) const; 560 Align2(T x)561 template <typename T> static constexpr inline T Align2(T x) { return (x + 1) >> 1 << 1; } Align4(T x)562 template <typename T> static constexpr inline T Align4(T x) { return (x + 3) >> 2 << 2; } Align8(T x)563 template <typename T> static constexpr inline T Align8(T x) { return (x + 7) >> 3 << 3; } 564 IsAlign2(T x)565 template <typename T> static constexpr inline bool IsAlign2(T x) { return (x & 1) == 0; } IsAlign4(T x)566 template <typename T> static constexpr inline bool IsAlign4(T x) { return (x & 3) == 0; } IsAlign8(T x)567 template <typename T> static constexpr inline bool IsAlign8(T x) { return (x & 7) == 0; } IsPow2(T value)568 template <typename T> static constexpr inline bool IsPow2(T value) 569 { 570 return (value & (value - 1)) == 0; 571 } AlignPtr(T x)572 template <typename T> static constexpr inline T AlignPtr(T x) 573 { 574 return sizeof(void*) == POINTSIZE8 ? Align8(x) : Align4(x); 575 } IsAlignPtr(T x)576 template <typename T> static constexpr inline bool IsAlignPtr(T x) 577 { 578 return sizeof(void*) == POINTSIZE8 ? IsAlign8(x) : IsAlign4(x); 579 } 580 581 template <typename T, typename... Args> Push(size_t pod,Args &&...args)582 void* Push(size_t pod, Args&&... args) 583 { 584 size_t skip = AlignPtr(sizeof(T) + pod); 585 ACE_DCHECK(skip < (1 << SKIPSIZE)); 586 if (fUsed + skip > fReserved) { 587 static_assert(IsPow2(PAGESIZE), "This math needs updating for non-pow2."); 588 fReserved = (fUsed + skip + PAGESIZE) & ~(PAGESIZE - 1); 589 fBytes.realloc(fReserved); 590 if (!fBytes.get()) { 591 TAG_LOGE(AceLogTag::ACE_CANVAS_COMPONENT, "realloc failed"); 592 } 593 } 594 ACE_DCHECK(fUsed + skip <= fReserved); 595 auto op = (T*)(fBytes.get() + fUsed); 596 fUsed += skip; 597 new (op) T{std::forward<Args>(args)...}; 598 op->type = (uint32_t)T::kType; 599 op->skip = skip; 600 return op + 1; 601 } 602 603 private: 604 AutoTMalloc<uint8_t> fBytes; 605 size_t fUsed = 0; 606 size_t fReserved = 0; 607 608 bool mHasText : 1; 609 static constexpr uint8_t SKIPSIZE = 24; 610 static constexpr uint8_t POINTSIZE8 = 8; 611 static constexpr uint16_t PAGESIZE = 4096; 612 }; 613 614 } // namespace OHOS::Ace::NG 615 616 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CANVAS_PAINT_OP_H 617