1 /* 2 * Copyright (c) 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 GRAPHIC_LITE_DRAW_CANVAS_H 17 #define GRAPHIC_LITE_DRAW_CANVAS_H 18 19 #include "common/image.h" 20 #include "gfx_utils/diagram/common/paint.h" 21 #include "gfx_utils/diagram/depiction/depict_dash.h" 22 #include "gfx_utils/diagram/depiction/depict_stroke.h" 23 #include "gfx_utils/diagram/rasterizer/rasterizer_scanline_antialias.h" 24 #include "gfx_utils/diagram/spancolorfill/fill_gradient_lut.h" 25 26 27 namespace OHOS { 28 29 #if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND 30 struct ImageParam : public HeapBase { 31 Point start; 32 uint16_t height; 33 uint16_t width; 34 int16_t newWidth; 35 int16_t newHeight; 36 Image* image; 37 }; 38 39 struct PathParam : public HeapBase { 40 UICanvasVertices* vertices; 41 ImageParam* imageParam = nullptr; 42 bool isStroke; 43 }; 44 #endif 45 46 class RenderBuffer; 47 class RenderBase; 48 class DrawCanvas : public HeapBase { 49 public: 50 #if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND 51 static void DoRender(BufferInfo& gfxDstBuffer, 52 void* param, 53 const Paint& paint, 54 const Rect& rect, 55 const Rect& invalidatedArea, 56 const Style& style, 57 const bool& isStroke); 58 59 #if defined(GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG) && GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG 60 static void DoDrawShadow(BufferInfo& gfxDstBuffer, 61 void* param, 62 const Paint& paint, 63 const Rect& rect, 64 const Rect& invalidatedArea, 65 const Style& style, 66 const bool& isStroke); 67 #endif 68 #endif 69 static void InitRenderAndTransform(BufferInfo& gfxDstBuffer, 70 RenderBuffer& renderBuffer, 71 const Rect& rect, 72 TransAffine& transform, 73 const Style& style, 74 const Paint& paint); 75 76 static void SetRasterizer(UICanvasVertices& vertices, 77 const Paint& paint, 78 RasterizerScanlineAntialias& rasterizer, 79 TransAffine& transform, 80 const bool& isStroke); 81 82 #if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG 83 /** 84 * Render gradient 85 */ 86 static void RenderGradient(const Paint& paint, 87 RasterizerScanlineAntialias& rasterizer, 88 TransAffine& transform, 89 RenderBase& renBase, 90 RenderBuffer& renderBuffer, 91 FillBase& allocator, 92 const Rect& invalidatedArea); 93 94 static void BuildGradientColor(const Paint& paint, FillGradientLut& gradientColorMode); 95 BuildLineGradientMatrix(const Paint & paint,TransAffine & gradientMatrix,TransAffine & transform,float & distance)96 static void BuildLineGradientMatrix(const Paint& paint, 97 TransAffine& gradientMatrix, 98 TransAffine& transform, 99 float& distance) 100 { 101 Paint::LinearGradientPoint linearPoint = paint.GetLinearGradientPoint(); 102 float angle = FastAtan2F(linearPoint.y1 - linearPoint.y0, linearPoint.x1 - linearPoint.x0); 103 gradientMatrix.Reset(); 104 gradientMatrix *= TransAffine::TransAffineRotation(angle); 105 gradientMatrix *= TransAffine::TransAffineTranslation(linearPoint.x0, linearPoint.y0); 106 gradientMatrix *= transform; 107 gradientMatrix.Invert(); 108 distance = Sqrt((linearPoint.x1 - linearPoint.x0) * (linearPoint.x1 - linearPoint.x0) + 109 (linearPoint.y1 - linearPoint.y0) * (linearPoint.y1 - linearPoint.y0)); 110 } 111 112 static void BuildRadialGradientMatrix(const Paint& paint, 113 TransAffine& gradientMatrix, 114 TransAffine& transform, 115 float& startRadius, 116 float& endRadius); 117 #endif // GRAPHIC_ENABLE_GRADIENT_FILL_FLAG 118 119 #if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG 120 #if defined(ENABLE_CANVAS_EXTEND) && ENABLE_CANVAS_EXTEND 121 /** 122 * Render pattern mode 123 */ 124 static void RenderPattern(const Paint& paint, 125 void* param, 126 RasterizerScanlineAntialias& rasterizer, 127 RenderBase& renBase, 128 FillBase& allocator, 129 const Rect& rect); 130 #endif 131 #endif // GRAPHIC_ENABLE_PATTERN_FILL_FLAG 132 ChangeColor(Rgba8T & color,ColorType colorType,uint8_t alpha)133 static void ChangeColor(Rgba8T& color, ColorType colorType, uint8_t alpha) 134 { 135 color.red = colorType.red; 136 color.green = colorType.green; 137 color.blue = colorType.blue; 138 color.alpha = alpha; 139 } 140 RenderBlendSolid(const Paint & paint,Rgba8T & color,const bool & isStroke)141 static void RenderBlendSolid(const Paint& paint, Rgba8T& color, const bool& isStroke) 142 { 143 if (isStroke) { 144 if (paint.GetStyle() == Paint::STROKE_STYLE || paint.GetStyle() == Paint::STROKE_FILL_STYLE) { 145 ChangeColor(color, paint.GetStrokeColor(), 146 static_cast<uint8_t>(paint.GetStrokeColor().alpha * paint.GetGlobalAlpha())); 147 } 148 } else { 149 if (paint.GetStyle() == Paint::FILL_STYLE || paint.GetStyle() == Paint::STROKE_FILL_STYLE) { 150 ChangeColor(color, paint.GetFillColor(), 151 static_cast<uint8_t>(paint.GetFillColor().alpha * paint.GetGlobalAlpha())); 152 } 153 } 154 } 155 156 /** 157 * Assembly parameter setting lineweight,LineCap,LineJoin 158 */ LineStyleCalc(DepictStroke<LineStyle> & strokeLineStyle,const Paint & paint)159 template <class LineStyle> static void LineStyleCalc(DepictStroke<LineStyle>& strokeLineStyle, const Paint& paint) 160 { 161 strokeLineStyle.SetWidth(paint.GetStrokeWidth()); // Line style related 162 #if defined(GRAPHIC_ENABLE_LINECAP_FLAG) && GRAPHIC_ENABLE_LINECAP_FLAG 163 strokeLineStyle.SetLineCap(paint.GetLineCap()); 164 #endif 165 #if defined(GRAPHIC_ENABLE_LINEJOIN_FLAG) && GRAPHIC_ENABLE_LINEJOIN_FLAG 166 strokeLineStyle.SetLineJoin(paint.GetLineJoin()); 167 if (paint.GetMiterLimit() > 0) { 168 strokeLineStyle.SetMiterLimit(paint.GetMiterLimit()); 169 } 170 #endif 171 }; 172 173 #if defined(GRAPHIC_ENABLE_DASH_GENERATE_FLAG) && GRAPHIC_ENABLE_DASH_GENERATE_FLAG 174 /** 175 * Set linedash style 176 */ LineDashStyleCalc(DepictDash & dashStyle,const Paint & paint)177 static void LineDashStyleCalc(DepictDash& dashStyle, const Paint& paint) 178 { 179 for (uint32_t i = 0; i < paint.GetLineDashCount(); i += TWO_STEP) { 180 dashStyle.AddDash(paint.GetLineDash()[i], paint.GetLineDash()[i + 1]); 181 } 182 dashStyle.DashStart(paint.GetLineDashOffset()); 183 }; 184 #endif 185 }; 186 } // namespace OHOS 187 #endif // GRAPHIC_LITE_DRAW_CANVAS_H 188