1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.. All rights reserved. 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 CORE_CANVAS_H 17 #define CORE_CANVAS_H 18 19 #include <memory> 20 21 #include "common/rs_macros.h" 22 #include "drawing/engine_adapter/impl_interface/core_canvas_impl.h" 23 #include "utils/drawing_macros.h" 24 #include "utils/rect.h" 25 #include "drawing/draw/hps_effect_types.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 namespace Drawing { 30 enum class SrcRectConstraint { 31 STRICT_SRC_RECT_CONSTRAINT, 32 FAST_SRC_RECT_CONSTRAINT, 33 }; 34 35 struct HpsBlurParameter { 36 Rect src; 37 Rect dst; 38 scalar sigma { 1E-6 }; 39 float saturation { 1.0 }; 40 float brightness { 1.0 }; HpsBlurParameterHpsBlurParameter41 HpsBlurParameter(const Rect& s, const Rect& d, const scalar& sgm, 42 float satura, float bright) 43 : src(s), dst(d), sigma(sgm), saturation(satura), brightness(bright) {} 44 }; 45 46 /** 47 * @brief PointMode: Selects if an array of points are drawn as discrete points, as lines, or as 48 * an open polygon. 49 */ 50 enum class PointMode { 51 POINTS_POINTMODE, // draw each point separately 52 LINES_POINTMODE, // draw each pair of points as a line segment 53 POLYGON_POINTMODE, // draw the array of points as a open polygon 54 }; 55 56 #undef TRANSPARENT 57 struct Lattice { 58 enum RectType : uint8_t { 59 DEFAULT = 0, 60 TRANSPARENT, 61 FIXEDCOLOR, 62 }; 63 std::vector<int> fXDivs; 64 std::vector<int> fYDivs; 65 std::vector<RectType> fRectTypes; 66 int fXCount; 67 int fYCount; 68 std::vector<RectI> fBounds; 69 std::vector<Color> fColors; 70 }; 71 72 enum CacheType : uint8_t { 73 UNDEFINED, // do not change current cache status 74 ENABLED, // explicitly enable cache 75 DISABLED, // explicitly disable cache 76 OFFSCREEN, // offscreen rendering 77 }; 78 79 class Surface; 80 81 /** 82 * @brief Contains the option used to create the layer. 83 */ 84 class DRAWING_API SaveLayerOps { 85 public: 86 // How to allocate layer 87 enum Flags { 88 INIT_WITH_PREVIOUS = 1 << 1, // create with previous contents 89 }; 90 SaveLayerOps()91 SaveLayerOps() : bounds_(nullptr), brush_(nullptr), saveLayerFlags_(0) {} 92 93 /** 94 * @param bounds The bounds of layer, may be nullptr. 95 * @param brush When restoring the current layer, attach this brush, may be nullptr. 96 * @param saveLayerFlags How to allocate layer. 97 */ 98 SaveLayerOps(const Rect* bounds, const Brush* brush, uint32_t saveLayerFlags = 0) bounds_(bounds)99 : bounds_(bounds), brush_(brush), saveLayerFlags_(saveLayerFlags) {} ~SaveLayerOps()100 ~SaveLayerOps() {} 101 102 /** 103 * @brief Gets the bounds of layer, may be nullptr. 104 * @return Returns the bounds of layer. 105 */ GetBounds()106 const Rect* GetBounds() const 107 { 108 return bounds_; 109 } 110 111 /** 112 * @brief Gets the brush of layer, may be nullptr. 113 * @return Returns the brush of layer. 114 */ GetBrush()115 const Brush* GetBrush() const 116 { 117 return brush_; 118 } 119 120 /** 121 * @brief Gets the options to modify layer. 122 * @return Returns the options to modify layer. 123 */ GetSaveLayerFlags()124 uint32_t GetSaveLayerFlags() const 125 { 126 return saveLayerFlags_; 127 } 128 129 private: 130 const Rect* bounds_; 131 const Brush* brush_; 132 uint32_t saveLayerFlags_; 133 }; 134 135 class DRAWING_API CoreCanvas { 136 public: 137 CoreCanvas(); 138 explicit CoreCanvas(DrawingType type); ~CoreCanvas()139 virtual ~CoreCanvas() {} 140 void Bind(const Bitmap& bitmap); 141 142 void BuildOverDraw(std::shared_ptr<Canvas> canvas); 143 GetDrawingType()144 virtual DrawingType GetDrawingType() const 145 { 146 return DrawingType::COMMON; 147 } 148 149 /** 150 * @brief record canvas status 151 * @param canvas canvas to be record 152 */ 153 void RecordState(Canvas* canvas); 154 155 /** 156 * @brief inherite canvas status 157 * @return canvas canvas to be inherited 158 */ 159 void InheriteState(Canvas* canvas); 160 161 /** 162 * @brief Gets the total matrix of Canvas to device. 163 * @return Returns the total matrix of Canvas to device. 164 */ 165 virtual Matrix GetTotalMatrix() const; 166 167 /** 168 * @brief Gets bounds of clip in local coordinates. 169 * @return Returns bounds of clip in local coordinates. 170 */ 171 virtual Rect GetLocalClipBounds() const; 172 173 /** 174 * @brief Gets bounds of clip in device coordinates. 175 * @return Returns bounds of clip in device coordinates. 176 */ 177 virtual RectI GetDeviceClipBounds() const; 178 179 /** 180 * @brief Gets bounds of clip in device coordinates with round in. 181 * @return Returns bounds of clip in device coordinates. 182 */ 183 virtual RectI GetRoundInDeviceClipBounds() const; 184 185 #ifdef RS_ENABLE_GPU 186 /** 187 * @brief Gets GPU context of the GPU surface associated with Canvas. 188 * @return Returns GPU context of the GPU surface associated with Canvas. 189 */ 190 virtual std::shared_ptr<GPUContext> GetGPUContext(); 191 #endif 192 193 /** 194 * @brief Gets width of Canvas. 195 * @return Returns width of Canvas. 196 */ 197 int32_t GetWidth() const; 198 199 /** 200 * @brief Gets height of Canvas. 201 * @return Returns height of Canvas. 202 */ 203 int32_t GetHeight() const; 204 205 /** 206 * @brief Gets ImageInfo of Canvas. 207 * @return Returns ImageInfo of Canvas. 208 */ 209 ImageInfo GetImageInfo(); 210 211 bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 212 int srcX, int srcY); 213 214 bool ReadPixels(const Bitmap& dstBitmap, int srcX, int srcY); 215 216 // shapes 217 /** 218 * @brief The shape of point drawn depends on pen. If pen is set to Pen::CapStyle::ROUND_CAP, 219 * draw a circle of diameter pen stroke width. If pen is set to Pen::CapStyle::SQUAER_CAP, 220 * draw a square of width and height pen stroke width. 221 * @param point top-left edge of circle or square 222 */ 223 virtual void DrawPoint(const Point& point); 224 225 /** 226 * @brief Describing a graph by combining directed vector fields. 227 * @param shape describes the combination of a group of sdf entities. 228 */ 229 virtual void DrawSdf(const SDFShapeBase& shape); 230 231 /** 232 * @brief If mode is LINES_POINTMODE, each pair of points draws a line segment. One line 233 * is drawn for every two points; each point is used once. If count is odd, the final point is ignored. 234 * If mode is POLYGON_POINTMODE, each adjacent pair of points draws a line segment. count minus one lines 235 * are drawn; the first and last point are used once. 236 * @param mode whether pts draws points or lines 237 * @param count number of points in the array 238 * @param pts array of points to draw 239 */ 240 virtual void DrawPoints(PointMode mode, size_t count, const Point pts[]); 241 242 /** 243 * @brief Draws line segment from startPt to endPt. 244 * @param startPt start of line segment 245 * @param endPt end of line segment 246 */ 247 virtual void DrawLine(const Point& startPt, const Point& endPt); 248 249 /** 250 * @brief If rectangle is stroked, use pen to stroke width describes the line thickness, 251 * else use brush to fill the rectangle. 252 * @param rect rectangle to draw 253 */ 254 virtual void DrawRect(const Rect& rect); 255 256 /** 257 * @brief If round rectangle is stroked, use pen to stroke width describes the line thickness, 258 * else use brush to fill the round rectangle. Round rect may represent a rectangle, circle, 259 * oval, uniformly rounded rectangle, or may have any combination of positive non-square radii 260 * for the four corners. 261 * @param roundRect RRect with up to eight corner radii to draw 262 */ 263 virtual void DrawRoundRect(const RoundRect& roundRect); 264 265 /** 266 * @brief Outer must contain inner or the drawing is undefined. If RRect is stroked, use pen to stroke 267 * width describes the line thickness. If stroked and RRect corner has zero length radii, 268 * Pen::JoinStyle can draw corners rounded or square. 269 * @param outer RRect outer bounds to draw 270 * @param inner RRect inner bounds to draw 271 */ 272 virtual void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner); 273 274 /** 275 * @brief Arc is part of oval bounded by oval, sweeping from startAngle to startAngle plus sweepAngle. 276 * startAngle and sweepAngle are in degrees. StartAngle of zero places start point at the right 277 * middle edge of oval. A positive sweepAngle places arc end point clockwise from start point; 278 * A negative sweepAngle places arc end point counterclockwise from start point. sweepAngle may 279 * exceed 360 degrees, a full circle. Draw a wedge that includes lines from oval center to arc end points 280 * @param oval rect bounds of oval containing arc to draw 281 * @param startAngle angle in degrees where arc begins 282 * @param sweepAngle sweep angle in degrees, positive is clockwise 283 */ 284 virtual void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle); 285 286 /** 287 * @brief Draw arc between end points. 288 * @param oval rect bounds of oval containing arc to draw 289 * @param startAngle angle in degrees where arc begins 290 * @param sweepAngle sweep angle in degrees, positive is clockwise 291 */ 292 virtual void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle); 293 294 /** 295 * @brief If oval is stroked, use pen to stroke width describes the line thickness, else use brush to fill the oval. 296 * @param oval rect bounds of oval 297 */ 298 virtual void DrawOval(const Rect& oval); 299 300 /** 301 * @brief If radius is zero or less, nothing is drawn. If circle is stroked, use pen to 302 * stroke width describes the line thickness, else use brush to fill the circle. 303 * @param centerPt circle center 304 * @param radius half the diameter of circle 305 */ 306 virtual void DrawCircle(const Point& centerPt, scalar radius); 307 308 /** 309 * @brief Path contains an array of path contour, each of which may be open or closed. 310 * If RRect is filled, Path::PathFillType determines whether path contour 311 * describes inside or outside of fill; If stroked, use pen to stroke width 312 * describes the line thickness, Pen::CapStyle describes line ends, and 313 * Pen::Join describes how corners are drawn. 314 * @param path Path to draw 315 */ 316 virtual void DrawPath(const Path& path); 317 318 virtual void DrawPathWithStencil(const Path& path, uint32_t stencilVal); 319 320 /** 321 * @brief Use brush to fill the canvas. 322 * @param brush used to fill Canvas 323 */ 324 virtual void DrawBackground(const Brush& brush); 325 326 virtual void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius, 327 Color ambientColor, Color spotColor, ShadowFlags flag); 328 329 virtual void DrawShadowStyle(const Path& path, const Point3& planeParams, const Point3& devLightPos, 330 scalar lightRadius, Color ambientColor, Color spotColor, ShadowFlags flag, bool isLimitElevation); 331 332 // color 333 /** 334 * @brief Fills clip with color color. Mode determines how ARGB is combined with destination. 335 * @param color ColorQuad representing unpremultiplied color. 336 * @param mode BlendMode used to combine source color and destination. 337 */ 338 virtual void DrawColor(ColorQuad color, BlendMode mode = BlendMode::SRC_OVER); 339 340 /** 341 * @brief Draws Region on the Canvas. 342 * @param region Region to draw. 343 */ 344 virtual void DrawRegion(const Region& region); 345 346 /** 347 * @brief Draws a Coons patch: the interpolation of four cubics with shared corners, associating a color, 348 * and optionally a texture Point, with each corner. Point array cubics specifies four Path cubic 349 * starting at the top-left corner, in clockwise order, sharing every fourth point. The last Path 350 * cubic ends at the first point. Color array color associates colors with corners in top-left, 351 * top-right, bottom-right, bottom-left order. If brush contains Shader, Point array texCoords maps 352 * Shader as texture to corners in top-left, top-right, bottom-right, bottom-left order. If 353 * texCoords is nullptr, Shader is mapped using positions (derived from cubics). 354 * @param cubics Path cubic array, sharing common points 355 * @param colors color array, one for each corner 356 * @param texCoords Point array of texture coordinates, mapping Shader to corners; may be nullptr 357 * @param mode combines patch's colors with Shader if present or brush opaque color if not. 358 * Ignored if colors is null. 359 */ 360 virtual void DrawPatch(const Point cubics[12], const ColorQuad colors[4], 361 const Point texCoords[4], BlendMode mode); 362 363 /** 364 * @brief If brush or pen contains an Shader and vertices does not contain texCoords, 365 * the shader is mapped using the vertices' positions. If vertices colors are defined 366 * in vertices, and brush/pen contains Shader, BlendMode mode combines vertices colors with Shader. 367 * MaskFilter and PathEffect on paint are ignored. 368 * @param vertices triangle mesh to draw 369 * @param mode combines vertices' colors with Shader if present or brush/pen opaque color if not. 370 * Ignored if the vertices do not contain color. 371 */ 372 virtual void DrawVertices(const Vertices& vertices, BlendMode mode); 373 374 /** 375 * @brief Draw image stretched proportionally to fit into Rect dst. IRect 376 * center divides the image into nine sections: four sides, four corners, and 377 * the center. Corners are unmodified or scaled down proportionately if their sides 378 * are larger than dst; center and four sides are scaled to fit remaining space, if any. 379 * Additionally transform draw using clip, Matrix, and optional brush. If brush is attached, 380 * apply ColorFilter, alpha, ImageFilter, and BlendMode. If image is COLORTYPE_ALPHA_8, apply Shader. 381 * If brush contains MaskFilter, generate mask from image bounds. Any MaskFilter on paint is 382 * ignored as is paint anti-aliasing state. 383 * @param image Image containing pixels, dimensions, and format 384 * @param center IRect edge of image corners and sides 385 * @param dst destination Rect of image to draw to 386 * @param filter what technique to use when sampling the image 387 * @param brush brush containing MaskFilter; or nullptr 388 */ 389 virtual void DrawImageNine(const Image* image, const RectI& center, const Rect& dst, 390 FilterMode filter, const Brush* brush = nullptr); 391 392 /** 393 * @brief Draws Image image stretched proportionally to fit into Rect dst. 394 * Canvas::Lattice lattice divides image into a rectangular grid. 395 * Each intersection of an even-numbered row and column is fixed; 396 * fixed lattice elements never scale larger than their initial 397 * size and shrink proportionately when all fixed elements exceed the bitmap 398 * dimension. All other grid elements scale to fill the available space, if any. 399 * 400 * If brush is attached, apply ColorFilter, alpha, ImageFilter, and 401 * BlendMode. If image is COLORTYPE_ALPHA_8, apply Shader. 402 * If brush contains MaskFilter, generate mask from image bounds. 403 * Any MaskFilter on paint is ignored as is paint anti-aliasing state. 404 * 405 * @param image Image containing pixels, dimensions, and format 406 * @param lattice division of bitmap into fixed and variable rectangles 407 * @param dst destination Rect of image to draw to 408 * @param filter what technique to use when sampling the image 409 * @param brush brush containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr 410 */ 411 virtual void DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst, 412 FilterMode filter); 413 414 // opinc calculate realdraw rect 415 virtual bool OpCalculateBefore(const Matrix& matrix); 416 virtual std::shared_ptr<Drawing::OpListHandle> OpCalculateAfter(const Rect& bound); 417 418 // image 419 /** 420 * @brief Draws many parts of the image (atlas) onto the canvas. 421 * This approach can be optimized when you want to draw many parts of an image on the canvas. 422 * Rect tex selects the area in the atlas, xform transforms each sprite individually rotating or zooming. 423 * MaskFilter and PathEffect on brush are ignored. 424 * 425 * The xform and tex list must contain count entries, and if the colors is present, 426 * it must be the same length as the other two lists, the max count supported is 2000. 427 * Optional parameter colors, if present, are applied to each sprite using BlendMode mode, treating 428 * sprite as source and colors as destination. 429 * Optional parameter cullRect, if present, provides boundary values rendered by all 430 * components of the atlas to be compared to the clip. 431 * 432 * @param atlas Image containing pixels, dimensions, and format 433 * @param xform RSXform mappings for sprites in atlas 434 * @param tex destination Rect of image to draw to 435 * @param colors for each sprite, blend with it using blendmode; or nullptr 436 * @param count the number of sprites to draw, the maximum is 2000 437 * @param mode used to combine colors with sprites 438 * @param sampling SamplingOptions used when sampling from the atlas image 439 * @param cullRect bounds of sprites for efficient clipping; or nullptr 440 */ 441 virtual void DrawAtlas(const Image* atlas, const RSXform xform[], const Rect tex[], const ColorQuad colors[], 442 int count, BlendMode mode, const SamplingOptions& sampling, const Rect* cullRect); 443 virtual void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py); 444 virtual void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling); 445 virtual void DrawImageWithStencil(const Image& image, const scalar px, const scalar py, 446 const SamplingOptions& sampling, uint32_t stencilVal); 447 virtual void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling, 448 SrcRectConstraint constraint = SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT); 449 virtual void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling); 450 451 /** 452 * @brief Clip and Matrix are unchanged by picture contents, as if Save() was called 453 * before and Restore() was called after DrawPicture(). Picture records a series of 454 * draw commands for later playback. 455 * @param picture recorded drawing commands to play 456 */ 457 virtual void DrawPicture(const Picture& picture); 458 459 // temporary interface. Support drawing of SkSVGDOM 460 virtual void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom); 461 462 // text 463 /** 464 * @brief blob contains glyphs, their positions, and paint attributes specific to text: 465 * Typeface, text size, text scale x, text skew x, anti-alias, fake bold, 466 * font embedded bitmaps, pen/brush full hinting spacing, LCD text, linear text, 467 * and subpixel text. TextEncoding must be set to TextEncoding::GLYPH_ID. 468 * Elements of pen/brush: anti-alias, BlendMode, color including alpha, 469 * ColorFilter, MaskFilter, PathEffect, Shader, and Brush::Style; apply to blob. 470 * If attach pen to draw text, set Pen::Cap, Pen::Join, and stroke width; 471 * apply to Path created from blob. 472 * @param blob glyphs, positions, and their paints' text size, typeface, and so on 473 * @param x horizontal offset applied to blob 474 * @param y vertical offset applied to blob 475 */ 476 virtual void DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y); 477 478 /** 479 * @brief blob contains glyphs, their positions, and paint attributes specific to text: 480 * Typeface, text size, text scale x, text skew x, anti-alias, fake bold, 481 * font embedded bitmaps, pen/brush full hinting spacing, LCD text, linear text, 482 * and subpixel text. TextEncoding must be set to TextEncoding::GLYPH_ID. 483 * Elements of pen/brush: anti-alias, BlendMode, color including alpha, 484 * ColorFilter, MaskFilter, PathEffect, Shader, and Brush::Style; apply to blob. 485 * If attach pen to draw text, set Pen::Cap, Pen::Join, and stroke width; 486 * apply to Path created from blob. 487 * @param blob glyphs, positions, and their paints' text size, typeface, and so on 488 * @param x horizontal offset applied to blob 489 * @param y vertical offset applied to blob 490 */ 491 void DrawSingleCharacter(int32_t unicode, const Font& font, scalar x, scalar y); 492 493 /** 494 * @brief Draws a single character with font feature. 495 * 496 * @param str Indicates the single character encoded in UTF-8. 497 * @param font Indicates the pointer to an <b>OH_Drawing_Font</b> object. 498 * @param x Indicates the horizontal offset applied to the single character. 499 * @param y Indicates the vertical offset applied to the single character. 500 * @param fontFeatures Indicates the pointer to an <b>OH_Drawing_FontFeatures</b> object. 501 */ 502 void DrawSingleCharacterWithFeatures(const char* str, const Font& font, scalar x, scalar y, 503 std::shared_ptr<Drawing::DrawingFontFeatures> fontFeatures); 504 505 // symbol 506 virtual void DrawSymbol(const DrawingHMSymbolData& symbol, Point locate); 507 508 // stencil culling 509 virtual void ClearStencil(const RectI& rect, uint32_t stencilVal); 510 511 // clip 512 /** 513 * @brief Replace the clipping area with the intersection or difference between the 514 * current clipping area and Rect, and use a clipping edge that is aliased or anti-aliased. 515 * @param rect To combine with clipping area. 516 * @param op To apply to clip. 517 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 518 */ 519 virtual void ClipRect(const Rect& rect, ClipOp op = ClipOp::INTERSECT, bool doAntiAlias = false); 520 521 /** 522 * @brief Replace the clipping area with the intersection or difference between the 523 * current clipping area and RectI, and use a clipping edge. 524 * @param rect To combine with clipping area. 525 * @param op To apply to clip. 526 */ 527 virtual void ClipIRect(const RectI& rect, ClipOp op = ClipOp::INTERSECT); 528 529 /** 530 * @brief Replace the clipping area with the intersection or difference of the 531 * current clipping area and Rect, and use a clipping edge that is aliased or anti-aliased. 532 * @param roundRect To combine with clip. 533 * @param op To apply to clip. 534 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 535 */ 536 virtual void ClipRoundRect(const RoundRect& roundRect, ClipOp op = ClipOp::INTERSECT, bool doAntiAlias = false); 537 538 virtual void ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias = false); 539 540 /* 541 * @brief Replace the clipping area with the intersection or difference of the 542 current clipping area and Path, and use a clipping edge that is aliased or anti-aliased. 543 * @param path To combine with clip. 544 * @param op To apply to clip. 545 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 546 */ 547 virtual void ClipPath(const Path& path, ClipOp op = ClipOp::INTERSECT, bool doAntiAlias = false); 548 549 /** 550 * @brief Replace the clipping area with the intersection or difference of the 551 * current clipping area and Region, and use a clipping edge that is aliased or anti-aliased. 552 * @param region To combine with clip. 553 * @param op To apply to clip.The default value is ClipOp::INTERSECT 554 */ 555 virtual void ClipRegion(const Region& region, ClipOp op = ClipOp::INTERSECT); 556 557 /** 558 * @brief Returns true if clip is empty. 559 * @return true if clip is empty 560 */ 561 virtual bool IsClipEmpty(); 562 563 /** 564 * @brief Returns true if clip is Rect and not empty. 565 * @return true if clip is rect and not empty 566 */ 567 virtual bool IsClipRect(); 568 569 /** 570 * @deprecated this interface will be remove in furture and SHOULD NOT be used anymore. 571 * @brief Reset Clip States. 572 */ 573 virtual void ResetClip(); 574 575 /** 576 * @brief Returns true if clip is empty Path path, transformed by Matrix, 577 * can be quickly determined to be outside of clip. 578 * @param path Rect to compare with path 579 * @return true if path, transformed by Matrix, does not intersect clip 580 */ 581 virtual bool QuickReject(const Path& path); 582 583 /** 584 * @brief Returns true if clip is empty Rect rect, transformed by Matrix, 585 * can be quickly determined to be outside of clip. 586 * @param rect Rect to compare with clip 587 * @return true if rect, transformed by Matrix, does not intersect clip 588 */ 589 virtual bool QuickReject(const Rect& rect); 590 591 // transform 592 /** 593 * @brief Replaces RSMatrix with matrix. Unlike Concat(), any prior matrix state is overwritten. 594 * @param matrix matrix to copy, replacing existing RSMatrix 595 */ 596 virtual void SetMatrix(const Matrix& matrix); 597 598 /** 599 * @brief Sets RSMatrix to the identity matrix. Any prior matrix state is overwritten. 600 */ 601 virtual void ResetMatrix(); 602 603 /** 604 * @brief Replaces RSMatrix with matrix premultiplied with existing RSMatrix. 605 * This has the effect of transforming the drawn geometry by matrix, before 606 * transforming the result with existing RSMatrix. 607 * @param matrix matrix to premultiply with existing RSMatrix 608 */ 609 virtual void ConcatMatrix(const Matrix& matrix); 610 611 /** 612 * @brief Translates RSMatrix by dx along the x-axis and dy along the y-axis. 613 * Mathematically, replaces RSMatrix with a translation matrix premultiplied with RSMatrix. 614 * This has the effect of moving the drawing by (dx, dy) before transforming the result with RSMatrix. 615 * @param dx distance to translate on x-axis 616 * @param dy distance to translate on y-axis 617 */ 618 virtual void Translate(scalar dx, scalar dy); 619 620 /** 621 * @brief Scales RSMatrix by sx on the x-axis and sy on the y-axis. 622 * Mathematically, replaces RSMatrix with a scale matrix premultiplied with RSMatrix. 623 * This has the effect of scaling the drawing by (sx, sy) before transforming the result with RSMatrix. 624 * @param sx amount to scale on x-axis 625 * @param sy amount to scale on y-axis 626 */ 627 virtual void Scale(scalar sx, scalar sy); 628 629 /** 630 * @brief Rotates Matrix by degrees. 631 * @param deg Amount to rotate, in degrees. 632 */ Rotate(scalar deg)633 void Rotate(scalar deg) 634 { 635 Rotate(deg, 0, 0); 636 } 637 638 /** 639 * @brief Rotates RSMatrix by degrees about a point at (sx, sy). Positive degrees rotates clockwise. 640 * Mathematically, constructs a rotation matrix; premultiplies the rotation matrix by 641 * a translation matrix; then replaces RSMatrix with the resulting matrix premultiplied with RSMatrix. 642 * This has the effect of rotating the drawing about a given point before transforming the result with RSMatrix. 643 * @param deg amount to rotate, in degrees 644 * @param sx x-axis value of the point to rotate about 645 * @param sy y-axis value of the point to rotate about 646 */ 647 virtual void Rotate(scalar deg, scalar sx, scalar sy); 648 649 /** 650 * @brief Shear RSMatrix by sx on the x-axis and sy on the y-axis. A positive value of sx 651 * skews the drawing right as y-axis values increase; a positive value of sy skews 652 * the drawing down as x-axis values increase. Mathematically, replaces RSMatrix with a 653 * skew matrix premultiplied with RSMatrix. 654 * This has the effect of skewing the drawing by (sx, sy) before transforming the result with RSMatrix. 655 * @param sx amount to skew on x-axis 656 * @param sy amount to skew on y-axis 657 */ 658 virtual void Shear(scalar sx, scalar sy); 659 660 // state 661 /** 662 * @brief Triggers the immediate execution of all pending draw operations. 663 * If Canvas is associated with GPU surface, resolves all pending GPU operations. 664 * If Canvas is associated with raster surface, has no effect; raster draw 665 * operations are never deferred. 666 */ 667 virtual void Flush(); 668 669 /** 670 * @brief Fills clip with color color using BlendMode::SRC. 671 * This has the effect of replacing all pixels contained by clip with color. 672 * @param color unpremultiplied ARGB 673 */ 674 virtual void Clear(ColorQuad color); 675 676 /** 677 * @brief Saves Matrix and clipping area, return the number of saved states. 678 */ 679 virtual uint32_t Save(); 680 681 /** 682 * @brief Saves Matrix and clipping area, and allocates Surface for subsequent drawing. 683 * @param saveLayerOps Contains the option used to create the layer. 684 */ 685 virtual void SaveLayer(const SaveLayerOps& saveLayerOps); 686 687 /** 688 * @brief Removes changes to Matrix and clip since Canvas state was last saved. 689 * The state is removed from the stack. Does nothing if the stack is empty. 690 */ 691 virtual void Restore(); 692 693 /** 694 * @brief Returns the number of saved states, each containing Matrix and clipping area. 695 * 696 * @return uint32_t type, represent depth of save state stack 697 */ 698 virtual uint32_t GetSaveCount() const; 699 700 /** 701 * @brief Makes Canvas contents undefined. 702 */ 703 virtual void Discard(); 704 705 // paint 706 /** 707 * @brief Attach pen to canvas and stroke something. 708 * @param pen tool to stroke 709 * @return CoreCanvas& 710 */ 711 virtual CoreCanvas& AttachPen(const Pen& pen); 712 713 /** 714 * @brief Attach brush to canvas and fill something. 715 * @param brush tool to fill 716 * @return CoreCanvas& 717 */ 718 virtual CoreCanvas& AttachBrush(const Brush& brush); 719 720 /** 721 * @brief Attach paint to canvas to draw something. 722 * @param paint tool to fill or stroke something 723 * @return CoreCanvas& 724 */ 725 virtual CoreCanvas& AttachPaint(const Paint& paint); 726 727 /** 728 * @brief Detach pen from canvas. 729 * @return CoreCanvas& 730 */ 731 virtual CoreCanvas& DetachPen(); 732 733 /** 734 * @brief Detach brush from canvas. 735 * @return CoreCanvas& 736 */ 737 virtual CoreCanvas& DetachBrush(); 738 739 /** 740 * @brief Detach paint from canvas. 741 * @return CoreCanvas& 742 */ 743 virtual CoreCanvas& DetachPaint(); 744 745 virtual ColorQuad GetEnvForegroundColor() const; 746 virtual bool isHighContrastEnabled() const; 747 virtual Drawing::CacheType GetCacheType() const; 748 virtual Drawing::Surface* GetSurface() const; 749 750 virtual float GetAlpha() const; 751 virtual int GetAlphaSaveCount() const; 752 753 template<typename T> GetImpl()754 T* GetImpl() const 755 { 756 return impl_->DowncastingTo<T>(); 757 } 758 std::shared_ptr<CoreCanvasImpl> GetCanvasData() const; 759 GetMutableBrush()760 Paint& GetMutableBrush() 761 { 762 return paintBrush_; 763 } 764 GetMutablePen()765 Paint& GetMutablePen() 766 { 767 return paintPen_; 768 } 769 770 virtual void SetParallelRender(bool parallelEnable); 771 772 virtual bool DrawBlurImage(const Image& image, const HpsBlurParameter& blurParams); 773 774 virtual bool DrawImageEffectHPS(const Image& image, 775 const std::vector<std::shared_ptr<Drawing::HpsEffectParameter>>& hpsEffectParams); 776 777 /** 778 * @brief Get the size after HPS blur downsampling. Only VK will return valid values. 779 * @param blurParams HPS blur Param is used to calculate the size after downsampling. 780 * @return {width, height}, if return {0, 0}, witch means something error. 781 */ 782 virtual std::array<int, 2> CalcHpsBluredImageDimension(const Drawing::HpsBlurParameter& blurParams); 783 784 protected: 785 CoreCanvas(int32_t width, int32_t height); 786 void BuildNoDraw(int32_t width, int32_t height); 787 void BuildStateRecord(int32_t width, int32_t height); 788 void BuildStateInherite(int32_t width, int32_t height); 789 void Reset(int32_t width, int32_t height); 790 Paint paintBrush_; 791 Paint paintPen_; 792 Paint defaultPaint_; 793 794 private: 795 std::shared_ptr<CoreCanvasImpl> impl_; 796 #ifdef RS_ENABLE_GPU 797 std::shared_ptr<GPUContext> gpuContext_; 798 #endif 799 }; 800 } // namespace Drawing 801 } // namespace Rosen 802 } // namespace OHOS 803 #endif 804