C++ SKIA路径绘制

SkPath类

SkPath结构

去除成员函数之后,我们看到SkPath包括这几个成员,注释中补充了说明

class SK_API SkPath {  
    //SkPath中的主要内容,SkAutoTUnref是自解引用,之所以这么设计,是为了复制SkPath时,省去份量较多的点复制(只复制引用)。  
    //由一系列线段组成  
    SkAutoTUnref<SkPathRef> fPathRef;  
  
  
    int                 fLastMoveToIndex;  
    uint8_t             fFillType;//如下四种类型之一  
    /*enum FillType { 
        kWinding_FillType,//绘制所有线段包围成的区域 
        kEvenOdd_FillType,//绘制被所有线段包围奇数次的区域) 
        kInverseWinding_FillType,//kWinding_FillType取反,即绘制不在该区域的点 
        kInverseEvenOdd_FillType//第二种type取反 
        }*/  
    mutable uint8_t     fConvexity;//凹凸性,临时计算  
    mutable uint8_t     fDirection;//方向,顺时针/逆时针,临时计算  
#ifdef SK_BUILD_FOR_ANDROID  
    const SkPath*       fSourcePath;//Hwui中使用,暂不关注  
#endif  
};  

关于 fFillType中 kWinding_FillType和 kEvenOdd_FillType的区别,可看SkPath::contains。这是判断点是否在不规则几何体内的经典代码(),很有参考意义。

C++ SKIA渲染架构

从渲染流程上分,Skia可分为如下三个层级:

  1. 指令层:SkPicture、SkDeferredCanvas->SkCanvas。 这一层决定需要执行哪些绘图操作,绘图操作的预变换矩阵,当前裁剪区域,绘图操作产生在哪些layer上,Layer的生成与合并。
  2. 解析层:SkBitmapDevice->SkDraw->SkScan、SkDraw1Glyph::Proc。 这一层决定绘制方式,完成坐标变换,解析出需要绘制的形体(点/线/规整矩形)并做好抗锯齿处理,进行相关资源解析并设置好Shader。
  3. 渲染层:SkBlitter->SkBlitRow::Proc、SkShader::shadeSpan。 这一层进行采样(如果需要),产生实际的绘制效果,完成颜色格式适配,进行透明度混合和抖动处理(如果需要)。

SKIA渲染

C++ SKIA绘制虚线

DashPathEffect是PathEffect类的一个子类,可以使paint画出类似虚线的样子,并且可以任意指定虚实的排列方式.

/** intervals: array containing an even number of entries (>=2), with
     the even indices specifying the length of "on" intervals, and the odd
     indices specifying the length of "off" intervals.
    count: number of elements in the intervals array
    phase: offset into the intervals array (mod the sum of all of the
     intervals).

    For example: if intervals[] = {10, 20}, count = 2, and phase = 25,
     this will set up a dashed path like so:
     5 pixels off
     10 pixels on
     20 pixels off
     10 pixels on
     20 pixels off
     ...
    A phase of -5, 25, 55, 85, etc. would all result in the same path,
     because the sum of all the intervals is 30.

    Note: only affects stroked paths.
*/
static SkPathEffect* Create(const SkScalar intervals[], int count, SkScalar phase)

以上是官方的代码注释。