Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2014 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef GrPathRendering_DEFINED
      9 #define GrPathRendering_DEFINED
     10 
     11 #include "SkPath.h"
     12 #include "GrPathRange.h"
     13 #include "GrPipeline.h"
     14 
     15 class GrGpu;
     16 class GrPath;
     17 class GrStencilSettings;
     18 class GrStyle;
     19 struct SkScalerContextEffects;
     20 class SkDescriptor;
     21 class SkTypeface;
     22 
     23 /**
     24  * Abstract class wrapping HW path rendering API.
     25  *
     26  * The subclasses of this class use the possible HW API to render paths (as opposed to path
     27  * rendering implemented in Skia on top of a "3d" HW API).
     28  * The subclasses hold the global state needed to render paths, including shadow of the global HW
     29  * API state. Similar to GrGpu.
     30  *
     31  * It is expected that the lifetimes of GrGpuXX and GrXXPathRendering are the same. The call context
     32  * interface (eg.  * the concrete instance of GrGpu subclass) should be provided to the instance
     33  * during construction.
     34  */
     35 class GrPathRendering {
     36 public:
     37     virtual ~GrPathRendering() { }
     38 
     39     typedef GrPathRange::PathIndexType PathIndexType;
     40 
     41     enum PathTransformType {
     42         kNone_PathTransformType,        //!< []
     43         kTranslateX_PathTransformType,  //!< [kMTransX]
     44         kTranslateY_PathTransformType,  //!< [kMTransY]
     45         kTranslate_PathTransformType,   //!< [kMTransX, kMTransY]
     46         kAffine_PathTransformType,      //!< [kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY]
     47 
     48         kLast_PathTransformType = kAffine_PathTransformType
     49     };
     50 
     51     static inline int PathTransformSize(PathTransformType type) {
     52         switch (type) {
     53             case kNone_PathTransformType:
     54                 return 0;
     55             case kTranslateX_PathTransformType:
     56             case kTranslateY_PathTransformType:
     57                 return 1;
     58             case kTranslate_PathTransformType:
     59                 return 2;
     60             case kAffine_PathTransformType:
     61                 return 6;
     62 
     63             default:
     64                 SK_ABORT("Unknown path transform type");
     65                 return 0;
     66         }
     67     }
     68 
     69     // No native support for inverse at this time
     70     enum FillType {
     71         /** Specifies that "inside" is computed by a non-zero sum of signed
     72             edge crossings
     73         */
     74         kWinding_FillType,
     75         /** Specifies that "inside" is computed by an odd number of edge
     76             crossings
     77         */
     78         kEvenOdd_FillType,
     79     };
     80 
     81     static const GrUserStencilSettings& GetStencilPassSettings(FillType);
     82 
     83     /**
     84      * Creates a new gpu path, based on the specified path and stroke and returns it.
     85      *
     86      * @param SkPath    the geometry.
     87      * @param GrStyle   the style applied to the path. Styles with non-dash path effects are not
     88      *                  allowed.
     89      * @return a new GPU path object.
     90      */
     91     virtual sk_sp<GrPath> createPath(const SkPath&, const GrStyle&) = 0;
     92 
     93     /**
     94      * Creates a range of gpu paths with a common style.
     95      *
     96      * @param PathGenerator class that generates SkPath objects for each path in the range.
     97      * @param GrStyle   the common style applied to each path in the range. Styles with non-dash
     98      *                  path effects are not allowed.
     99      * @return a new path range.
    100      */
    101     virtual sk_sp<GrPathRange> createPathRange(GrPathRange::PathGenerator*, const GrStyle&) = 0;
    102 
    103     /**
    104      * Creates a range of glyph paths, indexed by glyph id. The glyphs will have an
    105      * inverted y-direction in order to match the raw font path data.
    106      *
    107      * @param SkTypeface   Typeface that defines the glyphs.
    108      *                     If null, the default typeface will be used.
    109      *
    110      * @param SkDescriptor Additional font configuration that specifies the font's size,
    111      *                     stroke, and other flags. This will generally come from an
    112      *                     SkGlyphCache.
    113      *
    114      *                     It is recommended to leave this value null when possible, in
    115      *                     which case the glyphs will be loaded directly from the font's
    116      *                     raw path data and sized at SkPaint::kCanonicalTextSizeForPaths.
    117      *                     This will result in less memory usage and more efficient paths.
    118      *
    119      *                     If non-null, the glyph paths will match the font descriptor,
    120      *                     including with the stroke information baked directly into
    121      *                     the outlines.
    122      *
    123      * @param GrStyle      Common style that the GPU will apply to every path. Note that
    124      *                     if the glyph outlines contain baked-in styles from the font
    125      *                     descriptor, the GPU style will be applied on top of those
    126      *                     outlines.
    127      *
    128      * @return a new path range populated with glyphs.
    129      */
    130     sk_sp<GrPathRange> createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
    131                                     const SkDescriptor*, const GrStyle&);
    132 
    133     /** None of these params are optional, pointers used just to avoid making copies. */
    134     struct StencilPathArgs {
    135         StencilPathArgs(bool useHWAA,
    136                         GrRenderTargetProxy* proxy,
    137                         const SkMatrix* viewMatrix,
    138                         const GrScissorState* scissor,
    139                         const GrStencilSettings* stencil)
    140             : fUseHWAA(useHWAA)
    141             , fProxy(proxy)
    142             , fViewMatrix(viewMatrix)
    143             , fScissor(scissor)
    144             , fStencil(stencil) {
    145         }
    146         bool                     fUseHWAA;
    147         GrRenderTargetProxy*     fProxy;
    148         const SkMatrix*          fViewMatrix;
    149         const GrScissorState*    fScissor;
    150         const GrStencilSettings* fStencil;
    151     };
    152 
    153     void stencilPath(const StencilPathArgs& args, const GrPath* path);
    154 
    155     void drawPath(const GrPipeline& pipeline,
    156                   const GrPrimitiveProcessor& primProc,
    157                   const GrStencilSettings& stencilPassSettings, // Cover pass settings in pipeline.
    158                   const GrPath* path);
    159 
    160     void drawPaths(const GrPipeline& pipeline,
    161                    const GrPrimitiveProcessor& primProc,
    162                    const GrStencilSettings& stencilPassSettings, // Cover pass settings in pipeline.
    163                    const GrPathRange* pathRange,
    164                    const void* indices,
    165                    PathIndexType indexType,
    166                    const float transformValues[],
    167                    PathTransformType transformType,
    168                    int count);
    169 
    170 protected:
    171     GrPathRendering(GrGpu* gpu) : fGpu(gpu) { }
    172 
    173     virtual void onStencilPath(const StencilPathArgs&, const GrPath*) = 0;
    174     virtual void onDrawPath(const GrPipeline&,
    175                             const GrPrimitiveProcessor&,
    176                             const GrStencilSettings&,
    177                             const GrPath*) = 0;
    178     virtual void onDrawPaths(const GrPipeline&,
    179                              const GrPrimitiveProcessor&,
    180                              const GrStencilSettings&,
    181                              const GrPathRange*,
    182                              const void* indices,
    183                              PathIndexType,
    184                              const float transformValues[],
    185                              PathTransformType,
    186                              int count) = 0;
    187 
    188     GrGpu* fGpu;
    189 private:
    190     GrPathRendering& operator=(const GrPathRendering&);
    191 };
    192 
    193 #endif
    194